Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does erasure complicate implementing function types?

I read from an interview with Neal Gafter:

"For example, adding function types to the programming language is much more difficult with Erasure as part of Generics."

EDIT: Another place where I've met similar statement was in Brian Goetz's message in Lambda Dev mailing list, where he says that lambdas are easier to handle when they are just anonymous classes with syntactic sugar:

But my objection to function types was not that I don't like function types -- I love function types -- but that function types fought badly with an existing aspect of the Java type system, erasure. Erased function types are the worst of both worlds. So we removed this from the design.

Can anyone explain these statements? Why would I need runtime type information with lambdas?

like image 782
Aivar Avatar asked Sep 15 '11 19:09

Aivar


1 Answers

The way I understand it, is that they decided that thanks to erasure it would be messy to go the way of 'function types', e.g. delegates in C# and they only could use lambda expressions, which is just a simplification of single abstract method class syntax.

Delegates in C#:

public delegate void DoSomethingDelegate(Object param1, Object param2);
...
//now assign some method to the function type variable (delegate)
DoSomethingDelegate f = DoSomething;
f(new Object(), new Object());

(another sample here http://geekswithblogs.net/joycsharp/archive/2008/02/15/simple-c-delegate-sample.aspx)

One argument they put forward in Project Lambda docs:

Generic types are erased, which would expose additional places where developers are exposed to erasure. For example, it would not be possible to overload methods m(T->U) and m(X->Y), which would be confusing.

section 2 in: http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-3.html

(The final lambda expressions syntax will be a bit different from the above document: http://mail.openjdk.java.net/pipermail/lambda-dev/2011-September/003936.html)

(x, y) => { System.out.printf("%d + %d = %d%n", x, y, x+y); }

All in all, my best understanding is that only a part of syntax stuff that could, actually will be used. What Neal Gafter most likely meant was that not being able to use delegates will make standard APIs more difficult to adjust to functional style, rather than that javac/JVM update would be more difficult to be done.

If someone understands this better than me, I will be happy to read his account.

like image 137
MarianP Avatar answered Oct 20 '22 00:10

MarianP