Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "mumble typing?"

I've seen several mentions of "mumble typing," such as this StackOverflow answer: Will a future version of .NET support tuples in C#?

I Googled the term and I couldn't find much in the way of an explanation, other than some people hoping that C# would eventually support it.

What is mumble typing?

like image 223
David Pfeffer Avatar asked Apr 17 '11 03:04

David Pfeffer


2 Answers

I don't know if someone on the C# design team came up with this term, or if it is used elsewhere in the industry. We started using it while working on implicitly typed local variables (that is "var x = whatever;") in C# 3.0. A fairly common scenario is to want the compiler to infer part of a type but be explicit about another part. For example, consider the following:

var list = new List<int>() { 10, 20, 30 };

Here the type of "list" is not given, but the type argument of the generic list is given. One imagines that this could be inferred from the initializer:

var list = new List<???>() { 10, 20, 30 };

Here the ??? means "there's a type argument that goes here; compiler, figure out what it has to be from the context".

We call this "mumble typing" because one humourously imagines the code being read as "var list equals new list of hrmhmrhrm initialized with ten, twenty, thirty."

We never implemented mumble typing, which is a bit unfortunate because it means that it is hard to make a list of anonymous type. To do so, you can use the "cast by example" trick.

like image 191
Eric Lippert Avatar answered Sep 24 '22 03:09

Eric Lippert


I like this link, where the explanation is near the end of the page. Basically, my understanding is that the idea of "mumble typing" is type inference without having to specify the entire type. Now, C# 4.0 does have anonymous types, but there are limitations, some of which are explained in the above link and here.

I think the main problem in C# still is that, when you need to name a type, but you just have an anonymous type, there is no syntax that allows you to specify what you need. For example, this does not work:

List<MyObject> myList = SomeFunctionThatReturnsThisList(someParameter);
var afterTransformation = myList.Select<MyObject, var>(o => new { x = o.x, y = o.y });

You cannot specify var as a type in a generic. That was a somewhat foolish example, but I hope it conveys the idea that there are syntactic constructions with anonymous types that seem like they should be possible but are not.

like image 33
Andrew Avatar answered Sep 23 '22 03:09

Andrew