473,385 Members | 1,630 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,385 software developers and data experts.

redefinition of generic methods

it is obvious that you cannot have two [or more] methods that differ only on
returning value:

void F() { }
int F() { }

it is however interesting (and to me : surprizing) that this restriction
also applies to generic methods that differ on constraints:

T F<T>() where T : class { }
T F<T>() where T : struct { }

note that constraints are strictly exclusive here. it would be deterministic
then for the compiler to pick a method to call:

string s = F<string>(); // pick first F
int i = F<int>(); // pick second F

I do not think that this is a compiler-only issue, I belive it is rather
caused by IL limitations. However, since .NET constraints are significantly
limited, I belive it would be easy to determine if constraints are exclusive
or not in general case [Am I wrong?].

Wiktor Zychla

Apr 26 '06 #1
3 1907
Wouldn't this complicate type inference to be also constraint binding?
IMHO, if would complicate quite a lot the final type binding (lookup).
I don't think it's an IL limitation. You could have F<X>'1 and F<X>'2.

Laura

"Wiktor Zychla [C# MVP]" <wz*****@nospm.ii.uni.wroc.pl.nospm> ha scritto nel
messaggio news:Oa**************@TK2MSFTNGP05.phx.gbl...
it is obvious that you cannot have two [or more] methods that differ only
on returning value:

void F() { }
int F() { }

it is however interesting (and to me : surprizing) that this restriction
also applies to generic methods that differ on constraints:

T F<T>() where T : class { }
T F<T>() where T : struct { }

note that constraints are strictly exclusive here. it would be
deterministic then for the compiler to pick a method to call:

string s = F<string>(); // pick first F
int i = F<int>(); // pick second F

I do not think that this is a compiler-only issue, I belive it is rather
caused by IL limitations. However, since .NET constraints are
significantly limited, I belive it would be easy to determine if
constraints are exclusive or not in general case [Am I wrong?].

Wiktor Zychla

Apr 26 '06 #2
> Wouldn't this complicate type inference to be also constraint binding?
IMHO, if would complicate quite a lot the final type binding (lookup).
could you think of any specific example?
I don't think it's an IL limitation. You could have F<X>'1 and F<X>'2.


is it possible in the same class?

Wiktor Zychla
Apr 26 '06 #3
"Laura T." <laura_t@yahoodotdotcom> wrote:
Wouldn't this complicate type inference to be also constraint binding?
IMHO, if would complicate quite a lot the final type binding (lookup).
I don't think it's an IL limitation. You could have F<X>'1 and F<X>'2.


The x in `x refers to the number of generic parameters. Generic types in
..NET are overloaded on the basis of arity (number of parameters) only. So
some F<> is in CLR nomenclature F`1. F`2 would be some F<,>

-- Barry
Apr 26 '06 #4

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

Similar topics

3
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
3
by: SimonH | last post by:
Hi all, I would like to make a generic set of methods that could be called regardless of the database behind the scenes. One of the methods I would like would take a string sql statement and...
2
by: ljlevend | last post by:
I've noticed that in VS.NET 2.0 Beta 1 that none of the methods in System.Collections.Generic.List are overridable. In my app I currently have over 50 strongly typed ArrayLists that inherit from...
0
by: Mikkel Blanné | last post by:
I haven't been able to find any references to using this combination of technologies (remoting + generic methods + method overloading). I don't think the problem has to do with C#, but I couldn't...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.