Re: Overloading and method signatures.
From: Marcos Stefanakopolus (taruntius_at_hotmail.com)
Date: 03/08/05
- Next message: Marcos Stefanakopolus: ""polymorphic" methods?"
- Previous message: mikkel_strack: "Open form from remote call"
- In reply to: Bruce Wood: "Re: Overloading and method signatures."
- Next in thread: probashi: "Re: Overloading and method signatures."
- Messages sorted by: [ date ] [ thread ]
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.
>
- Next message: Marcos Stefanakopolus: ""polymorphic" methods?"
- Previous message: mikkel_strack: "Open form from remote call"
- In reply to: Bruce Wood: "Re: Overloading and method signatures."
- Next in thread: probashi: "Re: Overloading and method signatures."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|