Function Selection when Using Parameterized Types (Generics)

G

Guest

I'm confused about the program below. Based on my reading of the C# spec, I
don't think it should compile, but it does when using Beta 1. Could somebody
please explain the function selection rules to me?

Thanks,
--
David Douglass
MCSD for Microsoft .NET
===================================================
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace CSharpSamples {
class FunctionOverloads {
static void Main(string[] args) {
FunctionOverloads1<ClassA> x = new FunctionOverloads1<ClassA>();
ClassA A = new ClassA();
x.Function1(A);
Console.ReadLine();
}
public class ClassA {
}
public class FunctionOverloads1<typeParam> {

public void Function1(typeParam arg) {
Console.WriteLine("Function1(typeParam arg)");
Console.WriteLine("arg.GetType() = '" + arg.GetType().ToString() + "'");
}


public void Function1(ClassA arg) {
Console.WriteLine("Function1(ClassA arg)");
Console.WriteLine("arg.GetType() = '" + arg.GetType().ToString() + "'");
}

}
}
}
 
N

Nick Malik

care to share your confusion? I don't see what you are seeing that is
"wrong" with the code. What is it that you expect shouldn't compile?

--- Nick
 
G

Guest

The program as written calls the function that takes ClassA as an argument.
But why not call the other signature that takes the type parameter? If the
function that takes ClassA as an argument is commented out, the other
function is called. Based on my reading of 20.1.8 of the C# Language
Specification, this seems ambiguous. Isn't this the same as F1 example in
20.1.8?

Thanks,
Dave

Nick Malik said:
care to share your confusion? I don't see what you are seeing that is
"wrong" with the code. What is it that you expect shouldn't compile?

--- Nick

David Douglass said:
I'm confused about the program below. Based on my reading of the C# spec, I
don't think it should compile, but it does when using Beta 1. Could somebody
please explain the function selection rules to me?

Thanks,
--
David Douglass
MCSD for Microsoft .NET
===================================================
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace CSharpSamples {
class FunctionOverloads {
static void Main(string[] args) {
FunctionOverloads1<ClassA> x = new FunctionOverloads1<ClassA>();
ClassA A = new ClassA();
x.Function1(A);
Console.ReadLine();
}
public class ClassA {
}
public class FunctionOverloads1<typeParam> {

public void Function1(typeParam arg) {
Console.WriteLine("Function1(typeParam arg)");
Console.WriteLine("arg.GetType() = '" + arg.GetType().ToString() + "'");
}


public void Function1(ClassA arg) {
Console.WriteLine("Function1(ClassA arg)");
Console.WriteLine("arg.GetType() = '" + arg.GetType().ToString() + "'");
}

}
}
}
 
N

Nick Malik

Good catch Dave. I believe that you are correct. The code below should
throw a compiler error... According to the spec, it should not compile.

The good thing is that it calls the correct method. The method defined as
"Function1(typeParam arg)" should not be called as long as the other one is
defined with the same method signature. As you demonstrated, it is never
called as long as the other one is defined and the parameter type is of the
same type.

To be honest, there is power in the way that this is defined. It allows the
author of the generic definition to place a "type-specific" implementation
of "Function1" into the class, and allow a "catch-all" method to handle any
new or unexpected types. It is also, in all liklihood, encouraging sloppy
coding practices. Powerful but sloppy. That's a compelling combination,
huh?

If anyone has any further insight, I join Dave in asking that you chime in.

--- Nick

David Douglass said:
The program as written calls the function that takes ClassA as an argument.
But why not call the other signature that takes the type parameter? If the
function that takes ClassA as an argument is commented out, the other
function is called. Based on my reading of 20.1.8 of the C# Language
Specification, this seems ambiguous. Isn't this the same as F1 example in
20.1.8?

Thanks,
Dave

Nick Malik said:
care to share your confusion? I don't see what you are seeing that is
"wrong" with the code. What is it that you expect shouldn't compile?

--- Nick

I'm confused about the program below. Based on my reading of the C#
spec,
I
don't think it should compile, but it does when using Beta 1. Could somebody
please explain the function selection rules to me?

Thanks,
--
David Douglass
MCSD for Microsoft .NET
===================================================
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace CSharpSamples {
class FunctionOverloads {
static void Main(string[] args) {
FunctionOverloads1<ClassA> x = new FunctionOverloads1<ClassA>();
ClassA A = new ClassA();
x.Function1(A);
Console.ReadLine();
}
public class ClassA {
}
public class FunctionOverloads1<typeParam> {

public void Function1(typeParam arg) {
Console.WriteLine("Function1(typeParam arg)");
Console.WriteLine("arg.GetType() = '" + arg.GetType().ToString() + "'");
}


public void Function1(ClassA arg) {
Console.WriteLine("Function1(ClassA arg)");
Console.WriteLine("arg.GetType() = '" + arg.GetType().ToString() + "'");
}

}
}
}
 
G

Guest

I can't really accept this. Developing a language like C# isn't a corporate
IT project where you make it up as you go along. I'm going to consider this
a bug in beta 1 and submit it to Microsoft. At a minimum it's a bug in the
spec.

Can anybody from Microsoft chime in on this?

Dave

Nick Malik said:
Good catch Dave. I believe that you are correct. The code below should
throw a compiler error... According to the spec, it should not compile.

The good thing is that it calls the correct method. The method defined as
"Function1(typeParam arg)" should not be called as long as the other one is
defined with the same method signature. As you demonstrated, it is never
called as long as the other one is defined and the parameter type is of the
same type.

To be honest, there is power in the way that this is defined. It allows the
author of the generic definition to place a "type-specific" implementation
of "Function1" into the class, and allow a "catch-all" method to handle any
new or unexpected types. It is also, in all liklihood, encouraging sloppy
coding practices. Powerful but sloppy. That's a compelling combination,
huh?

If anyone has any further insight, I join Dave in asking that you chime in.

--- Nick

David Douglass said:
The program as written calls the function that takes ClassA as an argument.
But why not call the other signature that takes the type parameter? If the
function that takes ClassA as an argument is commented out, the other
function is called. Based on my reading of 20.1.8 of the C# Language
Specification, this seems ambiguous. Isn't this the same as F1 example in
20.1.8?

Thanks,
Dave

Nick Malik said:
care to share your confusion? I don't see what you are seeing that is
"wrong" with the code. What is it that you expect shouldn't compile?

--- Nick

I'm confused about the program below. Based on my reading of the C# spec,
I
don't think it should compile, but it does when using Beta 1. Could
somebody
please explain the function selection rules to me?

Thanks,
--
David Douglass
MCSD for Microsoft .NET
===================================================
#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace CSharpSamples {
class FunctionOverloads {
static void Main(string[] args) {
FunctionOverloads1<ClassA> x = new FunctionOverloads1<ClassA>();
ClassA A = new ClassA();
x.Function1(A);
Console.ReadLine();
}
public class ClassA {
}
public class FunctionOverloads1<typeParam> {

public void Function1(typeParam arg) {
Console.WriteLine("Function1(typeParam arg)");
Console.WriteLine("arg.GetType() = '" + arg.GetType().ToString() + "'");
}


public void Function1(ClassA arg) {
Console.WriteLine("Function1(ClassA arg)");
Console.WriteLine("arg.GetType() = '" + arg.GetType().ToString() + "'");
}

}
}
}
 

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