473,399 Members | 2,858 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,399 software developers and data experts.

Why can't overloads take into account the return type.

eg

void DoIt()
{
int i = FromString("1");
double d = FromString("1.1");
}

int FromString(string SomeValue)
{
return int.Parse(SomeValue);
}

double FromString(string SomeValue)
{
return double.Parse(SomeValue);
}

The return type is really just a byref param underneath anyway I presume.
Nov 17 '05
59 3389

"Michael C" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:uO**************@TK2MSFTNGP09.phx.gbl...
"Mike Schilling" <ms*************@hotmail.com> wrote in message
news:eG**************@tk2msftngp13.phx.gbl...
Now you're adding another meaning, which is "give the compiler a hint
about which overload to choose".


Isn't this the same thing:

DoSomething((long)1);


No, that actually does a conversion of 1 to 1L.
Nov 17 '05 #51
I think the straight answer to this question is that YES, a language can be
implemented such that the return type can be taken into account during
overload resolution. It's similar to asking if a language implementation may
include a return type as part of the method signature. The answer is also yes.

Now, to the academic arguments:
(i) Do we need it? I don't see much use for it. But that's only an opinion.
(ii) Will it cause problems? The ambiguity issues proposed so far does not
impress me. However, I have a more theoretic concern with semantics, ie: what
does the function do? A set of functions by the same name that takes the same
exact input but produces different results is an oxymoron. A named function
should do the same exact thing to the same exact set of inputs. If you want
the same set of inputs to produce different results, you should consider a
different function.

P

"Michael C" wrote:
eg

void DoIt()
{
int i = FromString("1");
double d = FromString("1.1");
}

int FromString(string SomeValue)
{
return int.Parse(SomeValue);
}

double FromString(string SomeValue)
{
return double.Parse(SomeValue);
}

The return type is really just a byref param underneath anyway I presume.

Nov 17 '05 #52

Well , I don't agree with your answer, primary reason is that when u design
a compiler then it is upto u , u can definitely overload on the basis of
return type as well using attibutes of "Attributes Context Free Grammer " .
But this probably requires an overhead & thus it increases complexity of
compiler. That 's why compiler writer don't normally prefer doing it.

I will appreciate any comments on it.

"Michael C" wrote:
eg

void DoIt()
{
int i = FromString("1");
double d = FromString("1.1");
}

int FromString(string SomeValue)
{
return int.Parse(SomeValue);
}

double FromString(string SomeValue)
{
return double.Parse(SomeValue);
}

The return type is really just a byref param underneath anyway I presume.

Nov 17 '05 #53
An overload is based on a methods signature, which is its name and its
parameters. When you call a function like:

MyFunction("Foo");

and then like:

MyFunction("Bar");

The compiler must make a distinction between the two in order to determine
which overload to call. In this case, the name and parameters are the same,
so the same method would be called.

The assignment, like the following

int x = MyFunction("AddNumbers");

occurs after the method call has been chosen. At this point, the compiler is
matching the return type of the method it chose based on the methods
signature and parameteres passed with the type the value is being assigned
to.

While you could argue that the compiler could see that you have 10 different
functions that take strings of the same name, and it could look to see what
type of return value you were looking for, this would remove the type safety
you are given in C#. This works fine for an untyped language where the type
does not matter. But why then would you care about the return type?

Hope this helps...

Frisky
"Zeeshan Shakeel" <Zeeshan Sh*****@discussions.microsoft.com> wrote in
message news:F0**********************************@microsof t.com...

Well , I don't agree with your answer, primary reason is that when u
design
a compiler then it is upto u , u can definitely overload on the basis of
return type as well using attibutes of "Attributes Context Free Grammer "
.
But this probably requires an overhead & thus it increases complexity of
compiler. That 's why compiler writer don't normally prefer doing it.

I will appreciate any comments on it.

"Michael C" wrote:
eg

void DoIt()
{
int i = FromString("1");
double d = FromString("1.1");
}

int FromString(string SomeValue)
{
return int.Parse(SomeValue);
}

double FromString(string SomeValue)
{
return double.Parse(SomeValue);
}

The return type is really just a byref param underneath anyway I presume.

Nov 17 '05 #54


Frisky wrote:
This works fine for an untyped language where the type
does not matter.


In untyped (and dynamicly-typed, are those really what you mean with
untyped?) languages overloading (static method-dispatch) is not possible
at all -- there are no types to dispatch on available ;)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #55
IMO, there's a much simpler answer to all of this.

Loose typing means a slow application.

FoxPro for years had no typing at all. A variable was typed once
something was stored in it and that variable's type could change simply
by assigning it a new value of a different type.

Why was it slower? Because FoxPro could not be compiled down to
machine code and an interpreter was always running, even at run time.

FWIW, even if the C# developers *could* come up with a way to support
loose typing of functions, I don't think that they would. It would
encourage bad code design.

Nov 17 '05 #56
> FoxPro for years had no typing at all.

That is not exactly right. Machine code and assembly have no typing at all,
everything is a byte. VBScript is type safe, just not strictly typed
(variables don't have explicit types, but the values they contain do). C is
strictly typed (all variables have a type), but not type safe (anything can
be cast into anything else, even if it makes no sense).

--
Jonathan Allen
"PMGuy" <ma***************@tjx.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
IMO, there's a much simpler answer to all of this.

Loose typing means a slow application.

FoxPro for years had no typing at all. A variable was typed once
something was stored in it and that variable's type could change simply
by assigning it a new value of a different type.

Why was it slower? Because FoxPro could not be compiled down to
machine code and an interpreter was always running, even at run time.

FWIW, even if the C# developers *could* come up with a way to support
loose typing of functions, I don't think that they would. It would
encourage bad code design.

Nov 17 '05 #57
> FoxPro for years had no typing at all.

That is not exactly right. Machine code and assembly have no typing at all,
everything is a byte. VBScript is type safe, just not strictly typed
(variables don't have explicit types, but the values they contain do). C is
strictly typed (all variables have a type), but not type safe (anything can
be cast into anything else, even if it makes no sense).

--
Jonathan Allen
"PMGuy" <ma***************@tjx.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
IMO, there's a much simpler answer to all of this.

Loose typing means a slow application.

FoxPro for years had no typing at all. A variable was typed once
something was stored in it and that variable's type could change simply
by assigning it a new value of a different type.

Why was it slower? Because FoxPro could not be compiled down to
machine code and an interpreter was always running, even at run time.

FWIW, even if the C# developers *could* come up with a way to support
loose typing of functions, I don't think that they would. It would
encourage bad code design.

Nov 17 '05 #58
:)

Very true! Sometimes I must type before I think.

Obviously I was just contrasting. (I guess it didn't work)

--
Frisky

Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
"Helge Jensen" <he**********@slog.dk> wrote in message
news:OM**************@TK2MSFTNGP15.phx.gbl...


Frisky wrote:
This works fine for an untyped language where the type does not matter.


In untyped (and dynamicly-typed, are those really what you mean with
untyped?) languages overloading (static method-dispatch) is not possible
at all -- there are no types to dispatch on available ;)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-

Nov 17 '05 #59
:)

Very true! Sometimes I must type before I think.

Obviously I was just contrasting. (I guess it didn't work)

--
Frisky

Intellectuals solve problems; geniuses prevent them. ~ Albert Einstein
"Helge Jensen" <he**********@slog.dk> wrote in message
news:OM**************@TK2MSFTNGP15.phx.gbl...


Frisky wrote:
This works fine for an untyped language where the type does not matter.


In untyped (and dynamicly-typed, are those really what you mean with
untyped?) languages overloading (static method-dispatch) is not possible
at all -- there are no types to dispatch on available ;)

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-

Nov 17 '05 #60

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

Similar topics

5
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
10
by: Grizlyk | last post by:
1. Can abybody explain me why C++ function can not be overloaded by its return type? Return type can has priority higher than casting rules. For example: char input(); //can be compiled to name...
9
by: hufaunder | last post by:
I have a class "TestSuper" that implements the interface "TestBase". The interface has a property of type "ReturnType". The class "TestSuper" does not return "ReturnType" but a derivation...
1
by: subramanian100in | last post by:
Consider the following piece of code: #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <cstddef> using namespace std;
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.