gpt4 book ai didi

java - 为什么删除会使实现函数类型复杂化?

转载 作者:搜寻专家 更新时间:2023-10-30 21:30:42 25 4
gpt4 key购买 nike

我从 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 ,他说当 lambda 只是带有语法糖的匿名类时,它们更容易处理:

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.

谁能解释一下这些说法?为什么我需要 lambda 的运行时类型信息?

最佳答案

我的理解是,由于删除,他们决定采用“函数类型”的方式会很困惑,例如C# 中的委托(delegate),它们只能使用lambda 表达式,这只是单个抽象方法 类语法的简化。

C# 中的委托(delegate):

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 最有可能的意思是,不能使用委托(delegate)将使标准 API 更难适应函数式风格,而不是说 javac/JVM 更新会更难完成。

如果有人比我更了解这一点,我会很乐意阅读他的帐户。

关于java - 为什么删除会使实现函数类型复杂化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7436664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com