Generics and method overloading

Z

Zach

Consider the following code:

void Test(int i)
{
System.Console.WriteLine("int function");
}

void Test(object o)
{
System.Console.WriteLine("object function");
}

void Generic<T>(T t)
{
Test(t);
}


If I execute the code

Generic<int>(5);

then "object function" is printed to the screen. I understand why, it
appears to treat generic types as first-class types and decides that
there must exist a compile-time conversion from EVERY legal value for
T (everything in this case, since there is no where clause) to the
overload.


My questions are:

1) is this theoretically necessary
2) What would be the implications of changing this so that the either
the correct overload is selected at runtime rather than at compile
time, or making it so that semantic checking on the code inside
Generic<T>() is done for individual values of T that the function is
instantiated with, and not for the "generic value T"?
 
J

Jon Skeet [C# MVP]

Zach said:
Consider the following code:

void Test(int i)
{
System.Console.WriteLine("int function");
}

void Test(object o)
{
System.Console.WriteLine("object function");
}

void Generic<T>(T t)
{
Test(t);
}


If I execute the code

Generic<int>(5);

then "object function" is printed to the screen. I understand why, it
appears to treat generic types as first-class types and decides that
there must exist a compile-time conversion from EVERY legal value for
T (everything in this case, since there is no where clause) to the
overload.

In particular, overloading is always performed at compile time, and the
void Generic<T>(T t) is *not* recompiled (in terms of IL) for the
My questions are:

1) is this theoretically necessary

Well, anything can be done in theory, but this behaviour is consistent
with the general principle of overloading being done at compile-time
and overriding being done at runtime within .NET.
2) What would be the implications of changing this so that the either
the correct overload is selected at runtime rather than at compile
time, or making it so that semantic checking on the code inside
Generic<T>() is done for individual values of T that the function is
instantiated with, and not for the "generic value T"?

Selecting it at runtime would either require using reflection within
the compiled code, or changing the CLR to support overloading at
runtime.

The idea of the Generic<T>() code being checked for individual values
of T doesn't fly: you don't know all the values of T that will be used
when Generic<T> is compiled. This is where .NET generics are
significantly different from C++ templates, which are sort of (very)
glorified macros.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top