List Info

Thread: Re: Strange behavior with method overload resolution




Re: Strange behavior with method overload resolution
user name
2008-04-11 23:03:56
And what you suggest actually works in well with my
suggestion:

Dim X as Object = Nothing
TryParse("", X)
TryParse(Of Object)("", X)

<Obsolete("Specify (Of Object) if you really mean
this", False)> _
function TryParse(byval Input As String,<Out> byref
Value As Object) As Boolean
  WL("Object direct")
  return True
end function

function TryParse(Of T)(byval Input As String,<Out>
byref Value As T) As Boolean
  WL("Type: ", gettype(T).Name)
  return True
end function

Compile Warning:
'Public Function TryParse(Input As String, ByRef Value As
Object) As
Boolean' is obsolete: 'Specify (Of Object) if you really
mean this'.

Output:
Object direct
Type: Object

I assume C# will perform the same.

-- 
Regards,
Mark Hurd, B.Sc.(Ma.)(Hons.)

On 12/04/2008, Sébastien Lorion <sebastien.loriongmail.com> wrote:
> Hum, I don't know how generalized that scenario is, but
it sure does
> not look clean to me and if I really want to do that, I
would use the
> more explicit call
>
> TryParse<object>(input, out value)
>
> to make clear my intention. Anyway, I can live with
that, I will just
> be more careful to watch for yet another trap (YAT).
>
> Sébastien
>
> On 4/11/08, Mark Hurd <markehurdgmail.com> wrote:
> > On 12/04/2008, Sébastien Lorion
<sebastien.loriongmail.com> wrote:
> >  > Well, in my code I have another method
similar to:
> >  >
> >  > void DoStuff(Type type, string input)
> >  > {
> >  >  object value;
> >  >  if (TryParse(input, out value))
> >  >  {
> >  >    // ...
> >  >  }
> >  > }
> >  >
> >  > I made a mistake and forgot to pass
"type" to TryParse. I discovered
> >  > the bug only at runtime when testing my
code. I can understand the
> >  > compiler behavior, but it is not what I
expect since I think in most
> >  > case, the programmer did not intend to do
that. A warning would be
> >  > nice here IMO.
> >  >
> >  > Sébastien
> >
> >
> > I assume the warning you're looking for is
something like "Generic
> >  type inferred as Object", which would be the
deliberate case for too
> >  many situations to be a warning. (Remember that
in most cases warnings
> >  are seen as "show stoppers" -- the only
valid final build is one
> >  without any warnings.)
> >
> >  However, if you want it to be a warning or
invalid for your code, add
> >  an overload:
> >
> >  [Obsolete("Is this what you really
meant?", false)]
> >  bool TryParse(string Input, out object Value)
> >
> >  And when the overload resolution matches that
signature, you'll get
> >  the Obsolete warning (or error if you pass True
in the
> >  ObsoleteAttribute reference).
> >
> >  Regards,
> >  Mark Hurd, B.Sc.(Ma.)(Hons.)
> >
> >  P.S. I've hand crafted the C# code above from
this hand crafted VB.NET:
> >
> >  <Obsolete("Is this what you really
meant?", False)> _
> >  Function TryParse(ByVal Input As String,
<Out()> ByRef Value As
> >  Object> As Boolean
> >
>
> --
> Sébastien
> www.sebastienlorion.com
>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )