19

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?

Community
  • 1
  • 1
David Pfeffer
  • 38,869
  • 30
  • 127
  • 202

2 Answers2

22

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.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Hm... what would the "hrmmhhhr" become in case of "non uniform" initializer lists, i.e. expressions like: `new List??> { 10, 20.3, 20u }`, or `new List??> { 10, "What?", new Uuid() }`? – Paul Michalik Apr 17 '11 at 09:25
  • @Eric Can't you do this: `var list = new[] { new { a = 10 }, new { a = 15 }, new { a = 20 } }.ToList();` isn't this enough to produce "list of anonymous type objects"? – Lasse V. Karlsen Apr 17 '11 at 12:31
  • @Paul, well this: `new[] { 10, 20.3, 20u }` ends up as a `double[]`, so I would assume the C# compiler would stay consistent and make it a `List` in that case. – Lasse V. Karlsen Apr 17 '11 at 13:09
  • 1
    However, `new[] { 10, "kk" }` fails with a "No best type found for implicitly-typed array" – Lasse V. Karlsen Apr 17 '11 at 13:10
  • 1
    @Paul: Lasse is correct. A subtle design principle of C# is that when we need to determine a "best" type from a set of types, we always pick one of those types. We never say "we have Giraffe and Tiger, therefore Mammal is best" because Mammal wasn't a choice. In your first case we'd pick double, and in the second case we'd give an error. – Eric Lippert Apr 17 '11 at 15:59
  • 4
    @Lasse: Indeed, that works, but it is a bit irksome having to create an array and then copy it to a list. And there are arbitrarily many generic data structures of the form Foo, and only a small number of ToFoo extension methods that take an array. The problem of having a type that is partially stated by the developer and partially deduced by the compiler is much more general than the specific example I gave. – Eric Lippert Apr 17 '11 at 16:03
  • @Eric, @Lasse: Yes, these results are as I'd intuitively expect. I've assumed that there are no general (Haskell-ish) rules for type inherence, considering all possible implicit or user provided type conversions... – Paul Michalik Apr 23 '11 at 10:35
  • Is mumble typing something that ever gets reconsidered at intervals (major versions, for example), or is it gone-gone? – Marc Gravell Oct 07 '11 at 16:44
  • @MarcGravell: On every major iteration we go down the full list of possible features again; sometimes features that simply did not fit in a previous version's schedule are a good fit for the next version. You'll note for instance that though "dynamic" was the premiere feature for C# 4.0, actually that release also had a bunch of smaller features that had been on the "these shortcomings are vexing our users" list since the C# 2.0 days -- like covariance. That said, the longer our users succeed without a small feature, the more evidence we have that it wasn't really necessary. – Eric Lippert Oct 07 '11 at 16:53
  • @Eric true enough; I'm extra thankful, then, that iterator blocks and captured variables made the 2.0 cut *despite* their inherent complexity - the workarounds for those are not pretty. – Marc Gravell Oct 07 '11 at 19:35
  • @Eric, my understand from [Wes's blog](http://blogs.msdn.com/b/wesdyer/archive/2007/02/11/baby-names-nameless-keys-and-mumbling.aspx) was that 'mumble-typing' was passing a dummy anonymous type to a method so that it could then return anonymous types. Would that just be a special case of what you're saying here? – Benjol Oct 10 '11 at 11:15
  • @Benjol: I think of the trick Wes describes as being called "cast by example", and that you have to do it because we don't have mumble typing built in to the language. I don't think there's a consensus on *precisely* what "mumble typing" means. – Eric Lippert Oct 10 '11 at 14:06
  • @Eric. Of *course* mumble typing is precisely defined. It means a type which is _mumble mumble_ where you don't state a _mumble mumble_. Clear as mud! – ShuggyCoUk Oct 10 '11 at 15:13
  • @EricLippert, anyway, your *mumble mumble* example is funny, because I've [recently used dynamic](http://stackoverflow.com/questions/7659001/have-i-implemented-y-combinator-using-c-dynamic-and-if-i-havent-what-is-it) precisely to mean "*I* have no idea what the type is, so work it out for yourself" :) – Benjol Oct 10 '11 at 20:04
1

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.

Andrew
  • 14,325
  • 4
  • 43
  • 64