加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 16:38:55 所属栏目: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
我从 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更新.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读