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中的String是不可变对象 在面向对象及函数编程语言中,不...
String, StringBuffer 和 StringBuilder 可变性 String不可变...
序列化:把对象转换为字节序列的过程称为对象的序列化. 反序...
先说结论,是对象!可以继续往下看 数组是不是对象 什么是对...
为什么浮点数 float 或 double 运算的时候会有精度丢失的风险...
面试题引入 这里引申出一个经典问题,看下面代码 Integer a ...