和
shared T f<T>(T t){
return t;
}
我期待像
mod.f_.f[Integer](1)
在scala中工作,[Integer]
类型参数在哪里。但它不接受类型参数。
以及为什么 scala 看到的是 getter 而不是函数
shared Integer(Integer) fi = f<Integer>;
我希望 Integer(Integer) 足以告诉编译器将函数公开给 scala。
我不了解 Scala,所以我只能在 Java 部分提供帮助,但这是从 Java 调用该函数的方法:
f_.f(Integer.$TypeDescriptor$, Integer.instance(1))
或者:
f_.<Integer>f(Integer.$TypeDescriptor$, Integer.instance(1))
Ceylon 具有具体化的泛型,因此类型参数会变成常规参数,并且您必须提供类型参数的类型描述符对象。Ceylon 类和接口有一个静态$TypeDescriptor$
成员,您可以使用它;我不知道其他东西(Java 类、联合和交集类型……)是如何工作的,但是您可以在 Ceylon 中编写等效的调用,并通过使用--verbose=code
.
这是一个完整的工作示例,其中包含您的f
函数和调用它的 Java 类G
:gist
Ceylon 实际上应该满足类型安全的要求并以安全的方式处理空值。但它不会在运行时。
所以,这个问题有一个非常简单的答案:
scala> mod.f_.f(null,3).
这真的很有趣。
当然,还有更多的乐趣:
scala> implicit def f(x:Any):com.redhat.ceylon.compiler.java.runtime.model.TypeDescriptor = com.redhat.ceylon.compiler.java.runtime.model.TypeDescriptor.klass(x.getClass)
scala> mod.f_.f(List(),3)
scala> mod.f_.f(List,3)
scala> mod.f_.f(Float,3)
...