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"?
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"?