java – 为什么擦除使实现函数类型变得复杂?

前端之家收集整理的这篇文章主要介绍了java – 为什么擦除使实现函数类型变得复杂?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我从 interview with Neal Gafter读:

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

编辑:
另一个遇到类似声明的地方是Brian Goetz的message in Lambda Dev mailing list,在那里他说,当他们只是匿名的语法糖类时,他们更容易处理:

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.

任何人都可以解释这些陈述吗?为什么我需要lambdas的运行时类型信息?

解决方法

我理解的方式是,他们决定,由于擦除,这将是凌乱的“功能类型”的方式,例如C#中的代表,他们只能使用lambda表达式,这只是简单的单一抽象方法类语法.

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());

(另一个样本在这里
http://geekswithblogs.net/joycsharp/archive/2008/02/15/simple-c-delegate-sample.aspx)

他们在Project Lambda文档中提出的一个论点:

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.

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

(最后的lambda表达式语法将与上述文档有些不同:
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); }

总而言之,我最好的理解是,只有一部分语法的东西可以,实际上将被使用.
Neal Gafter最有可能意味着无法使用委托将使标准API更难以适应功能性风格,而不是更难以完成javac / JVM更新.

如果有人比我更了解,我会很高兴看到他的帐户.

猜你在找的Java相关文章