473,386 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Overloading and method signatures.

So this is ok:

class foo {
int myMethod(string, int) { ... }
int myMethod(string, double) { ... }
}

But this is not:

class foo {
int myMethod(string) { ... }
double myMethod(string) { ... }
}

The compiler complains "Class 'foo' already defines a member called
'myMethod' with the same parameter types'. Well, yes that's true, but my
question is "why on earth is the return type not considered part of a
method's signature for purposes of overload disambiguation?"

Can someone with perhaps a deeper understanding of C# clue me in as to why
this is the case? When calling the method in contexts such as:

int I = myMethod("Bob");
double D = myMethod("Sue");

There's no ambiguity about which implementation is meant. In contexts such
as:

Decimal Num = 1.0 + myMethod("Dave") + 7;

The compiler could simply require a cast to disambiguiate which type is
intended. The C# compiler doesn't seem to have any problem requiring silly
casts in all sorts of other places, so why would it be so bad to require
this:

Decimal Num = 1.0 + (double)myMethod("Dave") + 7;

Perhaps I'm just irritated that C# is making my job more difficult at the
moment because return type is _not_ part of a method's signature, but I'd
like to understand why and hear any suggestions as to ways to handle
situations where you do want to have two methods with the same name that
differ only in return type.
Nov 16 '05 #1
7 2089
Hi,

very simple:

object o = foo_instance.myMethod( "which one to call?")

Now imagine the complexity when you return a reference type , this why you
need to differentiate in parameters and the return is not considered

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marcos Stefanakopolus" <ta*******@hotmail.com> wrote in message
news:42********@news.microsoft.com...
So this is ok:

class foo {
int myMethod(string, int) { ... }
int myMethod(string, double) { ... }
}

But this is not:

class foo {
int myMethod(string) { ... }
double myMethod(string) { ... }
}

The compiler complains "Class 'foo' already defines a member called
'myMethod' with the same parameter types'. Well, yes that's true, but my
question is "why on earth is the return type not considered part of a
method's signature for purposes of overload disambiguation?"

Can someone with perhaps a deeper understanding of C# clue me in as to why
this is the case? When calling the method in contexts such as:

int I = myMethod("Bob");
double D = myMethod("Sue");

There's no ambiguity about which implementation is meant. In contexts
such as:

Decimal Num = 1.0 + myMethod("Dave") + 7;

The compiler could simply require a cast to disambiguiate which type is
intended. The C# compiler doesn't seem to have any problem requiring
silly casts in all sorts of other places, so why would it be so bad to
require this:

Decimal Num = 1.0 + (double)myMethod("Dave") + 7;

Perhaps I'm just irritated that C# is making my job more difficult at the
moment because return type is _not_ part of a method's signature, but I'd
like to understand why and hear any suggestions as to ways to handle
situations where you do want to have two methods with the same name that
differ only in return type.

Nov 16 '05 #2
So you could say that problem is because the compiler will get confused
as it is being *helpful* and wants to do the implicit conversion.

I guess this is a choice made in designing the language. If it was
chosen to flag this as a compile time error, you would be able to have
method overloading w/same parameters but with different return type.
IIRC, this is how C++ behaves as well.

-----
Ajay Kalra
aj*******@yahoo.com

Nov 16 '05 #3
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.

Nov 16 '05 #4
Bruce Wood <br*******@canada.com> wrote:
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.


IL itself does, interestingly enough. The method calls specify the
return type of the called method.

The CTS allows it, but the CLS doesn't - in other words, any class
which has two methods defined which differ only by return type doesn't
have to be consumed by a CLS-compliant language.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
We need to add class name or instance ref in this code
Decimal Num = 1.0 + (double)myMethod("Dave") + 7;


Foo f = new Foo();

Decimal Num = 1.0 + (double) f.myMethod("Dave") + 7;

now the cast to double is invalid!!

Nov 16 '05 #6
> The compiler complains "Class 'foo' already defines a member called
'myMethod' with the same parameter types'. Well, yes that's true, but my question is "why on earth is the return type not considered part of a method's signature for purposes of overload disambiguation?" This is because, the compiler (any compiler i know of) doesnt force you
either to assign the return value that a method or function returns to
appropriate type.

wrt. the example oulined by you, i can do away as follows:

private void CallFoo()
{
myMethod('foo');
}

The callFoo() is really not concerned about the return value of
myMethod but is only making the call to myMethod(), there is no basis
here for the compiler to figure out at run time as to which version of
myMethod() is called.

Thanks,

Raaj, Umapathy.

Marcos Stefanakopolus wrote: So this is ok:

class foo {
int myMethod(string, int) { ... }
int myMethod(string, double) { ... }
}

But this is not:

class foo {
int myMethod(string) { ... }
double myMethod(string) { ... }
}

The compiler complains "Class 'foo' already defines a member called
'myMethod' with the same parameter types'. Well, yes that's true, but my question is "why on earth is the return type not considered part of a method's signature for purposes of overload disambiguation?"

Can someone with perhaps a deeper understanding of C# clue me in as to why this is the case? When calling the method in contexts such as:

int I = myMethod("Bob");
double D = myMethod("Sue");

There's no ambiguity about which implementation is meant. In contexts such as:

Decimal Num = 1.0 + myMethod("Dave") + 7;

The compiler could simply require a cast to disambiguiate which type is intended. The C# compiler doesn't seem to have any problem requiring silly casts in all sorts of other places, so why would it be so bad to require this:

Decimal Num = 1.0 + (double)myMethod("Dave") + 7;

Perhaps I'm just irritated that C# is making my job more difficult at the moment because return type is _not_ part of a method's signature, but I'd like to understand why and hear any suggestions as to ways to handle
situations where you do want to have two methods with the same name that differ only in return type.


Nov 16 '05 #7
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" <br*******@canada.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.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.

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Iyer, Prasad C | last post by:
I am new to python. I have few questions a. Is there something like function overloading in python? b. Can I overload __init__ method Thanks in advance regards
6
by: james b | last post by:
Hi all, I'm trying to do something with method overloading and I can't seem to get it to work my code is along the lines of public int method1(int a, int b, int c){ //method body } public...
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
6
by: Armin Zingler | last post by:
Hi group, why can't I overload these procedures? Private Sub proc(ByVal arg As String()) End Sub Private Sub proc(ByVal ParamArray arg As String()) End Sub
6
by: Fernando Berretta | last post by:
Hello, Is there some way to truly Overload a WebMethod (Without using MessageName) ?? Thanks in advance, Fernando
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
15
by: AJ | last post by:
Why does the following behave as it does...? public class Base { public void Add( byte b ) { } } public class TestClass : Base
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.