Re: Overloading and method signatures.

From: Marcos Stefanakopolus (taruntius_at_hotmail.com)
Date: 03/08/05


Date: Tue, 8 Mar 2005 11:11:28 -0800

Thanks to everyone who replied. After thinking about it, I realized that
some part of my brain has been spoiled by Perl. Perl provides an expression
context when evaluating expressions, so that code can check whether it's
supposed to return a number or a string or a list or whatever, and then do
the right thing. C# acts in a vaguely similar way when it lets you say
things like:

int theNumber = 7;
Console.WriteLine("the number is " + theNumber);

Knowing that the expression is in "string context", the compiler calls
ToString on theNumber for you. Nice feature, but it led me to thinking that
knowledge of the calling context was something that the compiler would take
into account in a more general way. Sadly, not the case.

I'll post a separate thread to discuss possible work-arounds.

"Bruce Wood" <brucewood@canada.com> wrote in message
news:1110001725.161641.28580@f14g2000cwb.googlegroups.com...
> The simple answer is because of precisely the example you gave: there
> are many situations in which the compiler can't determine which one to
> call.
>
> The more complex answer involves looking at the solution you proposed,
> and seeing it from the compiler's point of view. You suggested this bit
> of code to disambiguate the situation:
>
> class foo {
> int myMethod(string) { ... }
> double myMethod(string) { ... }
> }
>
> Decimal Num = 1.0 + (double)myMethod("Dave") + 7;
>
> where the cast to (double) tells the compiler which method to call.
> However, compilers don't see things that way.
>
> As soon as you evaluate 'myMethod("Dave")', it becomes a value to be
> used in a calculation. The compiler doesn't "look ahead," as such, to
> see what kind of value it's going to need in the expression, or to what
> kind of value this value is going to be cast. That's all "in the
> future" as it were.
>
> This isn't unique to C#, by the way. I know of no language that allows
> overloads that will distinguish between two methods based on their
> return values alone. In particular, C++ and Java, which provided
> inspiration for C#, don't allow it, either.
>



Relevant Pages

  • Re: Overloading and method signatures.
    ... some part of my brain has been spoiled by Perl. ... Console.WriteLine("the number is " + theNumber); ... Knowing that the expression is in "string context", the compiler calls ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem with Hashes
    ... > well don't use it in 'this kind' context and the compiler will let you know, ... Context is very important in Perl. ... Cocoa programming in Perl: http://camelbones.sourceforge.net ...
    (comp.lang.perl.misc)
  • Re: Fwd: DBD::Informix A configuration failure
    ... Perl doesn't think your compiler handles prototypes. ... Testing whether your Informix test environment will work... ... >> problems with the test program, you will need to revisit this ...
    (perl.dbi.users)
  • Re: ANNOUNCE: DBD:Oracle 1.18
    ... Well a warning in one compiler is better than and error in an other so I ... This might be a problem more with the older oracle OCI in your client. ... perl -Mblib t/26exe_array.t ...
    (perl.dbi.users)
  • Re: Compiler and an interpreter
    ... >>> error.Can anybody please tell me how the compiler and interpreter will ... Some small unit of the language is an acceptable input. ... > Perl does not to my knowledge in any way require the reading of the ...
    (comp.programming)