Max Lybbert
Posts: 314
Nickname: mlybbert
Registered: Apr, 2005
|
|
Re: Elliotte Rusty Harold on Type Inference for Java, and on Moving On
|
Posted: Apr 26, 2007 8:33 PM
|
|
/* [Me] Given a particular literal (say, {4, "hello"}) how can I infer the correct class for that object?
[Response] Well in C# ...
var a = new {id=4,message="hello"};
Console.WriteLine(a.GetType());
<>f__AnonymousType0`2[System.Int32,System.String]
*/
If you're going to create a new type for every literal, then the answer is trivial. If you're going to try to choose the type from those already defined at the literal's point of instantiation, then you've got a mess on your hands. Especially in a relatively common case of multiple classes having the same data members but different methods/behaviors.
The only answer I can see is to annotate the correct type, which means "don't use type inference in that case."
|
|