微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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更新.

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

原文地址:https://www.jb51.cc/java/124715.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐