473,748 Members | 5,230 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading

Hi,

Why can I not overload on just the return type?

Say for example.

public int blah(int x)
{
}

and

public double blah(int x)
{
}
Why not, seems quite straight forward to me, why is this a limitation on C#?
Nov 15 '05 #1
31 2291
This is not a limitation of C#, it's not possible in VB.NET too, so I think
it's a IL/CLR limitation. In my opinion it wouldn't make sense to overload a
method only by it's return type. For example, lets say you have an
overloaded method TestMe that returns either a string value or a long value:

string s = TestMe(); ///This is quite straight-forward
object o = TestMe(); ///Would be a problem.
--
Greetz

Jan Tielens
_______________ _______________ __
Read my weblog: http://weblogs.asp.net/jan

<di********@dis cussion.microso ft.com> wrote in message
news:O0******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

Why can I not overload on just the return type?

Say for example.

public int blah(int x)
{
}

and

public double blah(int x)
{
}
Why not, seems quite straight forward to me, why is this a limitation on C#?

Nov 15 '05 #2
<di********@dis cussion.microso ft.com> wrote:
Why can I not overload on just the return type?

Say for example.

public int blah(int x)
{
}

and

public double blah(int x)
{
}
Why not, seems quite straight forward to me, why is this a limitation on C#?


Which version would get called here:

blah(5);

?

Or how about:

Console.WriteLi ne (blah(5));

?

Basically, it introduces an awful lot of ambiguity - and ends up with
an expression without a specific type, which doesn't occur anywhere
else in the language.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Good Q in fact.
Lemme take a shot at it:
As I understand, while a function *prototype* describes the return type and
parameters, a function signature *does not include* return types. And
functiona overloading *defines* only functions of different signatures can
be overloaded.
Please correct me if am wrong here.

TIA
Krishnan
<di********@dis cussion.microso ft.com> wrote in message
news:O0******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

Why can I not overload on just the return type?

Say for example.

public int blah(int x)
{
}

and

public double blah(int x)
{
}
Why not, seems quite straight forward to me, why is this a limitation on C#?

Nov 15 '05 #4
No, threre's no such restriction at the IL level, but other managed MSFT
languages choose not to support it.

Willy.

"Jan Tielens" <ja*@no.spam.pl ease.leadit.be> wrote in message
news:ul******** ******@TK2MSFTN GP11.phx.gbl...
This is not a limitation of C#, it's not possible in VB.NET too, so I think it's a IL/CLR limitation. In my opinion it wouldn't make sense to overload a method only by it's return type. For example, lets say you have an
overloaded method TestMe that returns either a string value or a long value:
string s = TestMe(); ///This is quite straight-forward
object o = TestMe(); ///Would be a problem.
--
Greetz

Jan Tielens
_______________ _______________ __
Read my weblog: http://weblogs.asp.net/jan

<di********@dis cussion.microso ft.com> wrote in message
news:O0******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

Why can I not overload on just the return type?

Say for example.

public int blah(int x)
{
}

and

public double blah(int x)
{
}
Why not, seems quite straight forward to me, why is this a limitation on

C#?


Nov 15 '05 #5

<di********@dis cussion.microso ft.com> wrote in message
news:O0******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

Why can I not overload on just the return type?


I believe Eiffel supports this
Nov 15 '05 #6
True, and ILASM also, here's a sample (compile with ilasm).

..assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 1:0:5000:0
}

..assembly invaril
{
.hash algorithm 0x00008004
.ver 0:0:0:1
}

..class private Invariant
{
.method public specialname
instance void .ctor()
{
ret
}

.method public instance int32 Foo()
{
.locals init (int32 local0)
ldc.i4.1
ret //return 1
}

.method public instance int64 Foo()
{
.locals init (int64 local0)
ldc.i4.2
conv.i8
ret // return 2
}
}

..method public static void Execute()
{
.entrypoint
.locals init (class Invariant local0)
newobj instance void Invariant::.cto r()
stloc.0
ldloc.0
//Call int32::Foo()
callvirt instance int32 Invariant::Foo( )
call void [mscorlib]System.Console: :WriteLine(int3 2)
ldloc.0
// Call int64::Foo()
callvirt instance int64 Invariant::Foo( )
call void [mscorlib]System.Console: :WriteLine(int6 4)
ret
}
Willy.

"andrew lowe" <andrew dot lowe at geac dot com> wrote in message
news:uU******** ******@tk2msftn gp13.phx.gbl...

<di********@dis cussion.microso ft.com> wrote in message
news:O0******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

Why can I not overload on just the return type?


I believe Eiffel supports this

Nov 15 '05 #7
It SHOULD be supported as its technically a DIFFERENT function signiture.

I think its stupid not to support this. Is there any valid reason why this
is not implemented?

I cant think of one.

"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:ub******** ******@TK2MSFTN GP11.phx.gbl...
True, and ILASM also, here's a sample (compile with ilasm).

.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 1:0:5000:0
}

.assembly invaril
{
.hash algorithm 0x00008004
.ver 0:0:0:1
}

.class private Invariant
{
.method public specialname
instance void .ctor()
{
ret
}

.method public instance int32 Foo()
{
.locals init (int32 local0)
ldc.i4.1
ret //return 1
}

.method public instance int64 Foo()
{
.locals init (int64 local0)
ldc.i4.2
conv.i8
ret // return 2
}
}

.method public static void Execute()
{
.entrypoint
.locals init (class Invariant local0)
newobj instance void Invariant::.cto r()
stloc.0
ldloc.0
//Call int32::Foo()
callvirt instance int32 Invariant::Foo( )
call void [mscorlib]System.Console: :WriteLine(int3 2)
ldloc.0
// Call int64::Foo()
callvirt instance int64 Invariant::Foo( )
call void [mscorlib]System.Console: :WriteLine(int6 4)
ret
}
Willy.

"andrew lowe" <andrew dot lowe at geac dot com> wrote in message
news:uU******** ******@tk2msftn gp13.phx.gbl...

<di********@dis cussion.microso ft.com> wrote in message
news:O0******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

Why can I not overload on just the return type?


I believe Eiffel supports this


Nov 15 '05 #8

"Alvin Bruney" <alvin.bruney@. telia..com.> wrote in message
news:O6******** ******@TK2MSFTN GP11.phx.gbl...
It SHOULD be supported as its technically a DIFFERENT function signiture.

I think its stupid not to support this. Is there any valid reason why this
is not implemented?

I cant think of one.

Its pretty simple, it heavily complicates the language for very little
benifit. I don't care to have to specify return type on every call, doubt
you would either. Even with defaults it is a messy situation.
Do you have a suggestion as to how to implement it without causing a major
complexity increase?
"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:ub******** ******@TK2MSFTN GP11.phx.gbl...
True, and ILASM also, here's a sample (compile with ilasm).

.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 1:0:5000:0
}

.assembly invaril
{
.hash algorithm 0x00008004
.ver 0:0:0:1
}

.class private Invariant
{
.method public specialname
instance void .ctor()
{
ret
}

.method public instance int32 Foo()
{
.locals init (int32 local0)
ldc.i4.1
ret //return 1
}

.method public instance int64 Foo()
{
.locals init (int64 local0)
ldc.i4.2
conv.i8
ret // return 2
}
}

.method public static void Execute()
{
.entrypoint
.locals init (class Invariant local0)
newobj instance void Invariant::.cto r()
stloc.0
ldloc.0
//Call int32::Foo()
callvirt instance int32 Invariant::Foo( )
call void [mscorlib]System.Console: :WriteLine(int3 2)
ldloc.0
// Call int64::Foo()
callvirt instance int64 Invariant::Foo( )
call void [mscorlib]System.Console: :WriteLine(int6 4)
ret
}
Willy.

"andrew lowe" <andrew dot lowe at geac dot com> wrote in message
news:uU******** ******@tk2msftn gp13.phx.gbl...

<di********@dis cussion.microso ft.com> wrote in message
news:O0******** ******@TK2MSFTN GP09.phx.gbl...
> Hi,
>
> Why can I not overload on just the return type?

I believe Eiffel supports this



Nov 15 '05 #9
You already do have to SPECIFY RETURN TYPES.

"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:Oa******** ******@TK2MSFTN GP10.phx.gbl...

"Alvin Bruney" <alvin.bruney@. telia..com.> wrote in message
news:O6******** ******@TK2MSFTN GP11.phx.gbl...
It SHOULD be supported as its technically a DIFFERENT function signiture.
I think its stupid not to support this. Is there any valid reason why this is not implemented?

I cant think of one.


Its pretty simple, it heavily complicates the language for very little
benifit. I don't care to have to specify return type on every call, doubt
you would either. Even with defaults it is a messy situation.
Do you have a suggestion as to how to implement it without causing a major
complexity increase?
"Willy Denoyette [MVP]" <wi************ *@pandora.be> wrote in message
news:ub******** ******@TK2MSFTN GP11.phx.gbl...
True, and ILASM also, here's a sample (compile with ilasm).

.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 1:0:5000:0
}

.assembly invaril
{
.hash algorithm 0x00008004
.ver 0:0:0:1
}

.class private Invariant
{
.method public specialname
instance void .ctor()
{
ret
}

.method public instance int32 Foo()
{
.locals init (int32 local0)
ldc.i4.1
ret //return 1
}

.method public instance int64 Foo()
{
.locals init (int64 local0)
ldc.i4.2
conv.i8
ret // return 2
}
}

.method public static void Execute()
{
.entrypoint
.locals init (class Invariant local0)
newobj instance void Invariant::.cto r()
stloc.0
ldloc.0
//Call int32::Foo()
callvirt instance int32 Invariant::Foo( )
call void [mscorlib]System.Console: :WriteLine(int3 2)
ldloc.0
// Call int64::Foo()
callvirt instance int64 Invariant::Foo( )
call void [mscorlib]System.Console: :WriteLine(int6 4)
ret
}
Willy.

"andrew lowe" <andrew dot lowe at geac dot com> wrote in message
news:uU******** ******@tk2msftn gp13.phx.gbl...
>
> <di********@dis cussion.microso ft.com> wrote in message
> news:O0******** ******@TK2MSFTN GP09.phx.gbl...
> > Hi,
> >
> > Why can I not overload on just the return type?
>
> I believe Eiffel supports this
>
>



Nov 15 '05 #10

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

Similar topics

17
4722
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense it's used in other languages supporting overloading (such as C++ and Java). As one of the...
4
6481
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
5
5246
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched around for about the last too weeks and have read up on new and overloading it to some extent but...
39
2192
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any C implementation supporting this feature? I assume some of you will claim that there is no need in function overloading, so I would like to know your arguments too. Thanks,
45
3290
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
2
2257
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It uses map<and heavy overloading. The problem is, the output file differs from input, and for the love of me I can't figure out why ;p #include <iostream> #include <fstream> #include <sstream>
15
2785
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
11
28141
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
10
3489
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.