Scala 2.12:什么是 Java 8 通用量化 SAM 特征的方法参考?
Scala 2.12: What is the equivalent of Java 8 method reference for universally quantified SAM trait?
我的 objective 是通过新的 scala 2.12 支持 SAM(单一抽象方法)特征来实现代数数据类型(教会编码)的单例值。
在Java中,如下程序returntrue
:
import java.util.function.Function;
import java.util.function.Supplier;
@FunctionalInterface
public interface Maybe<A> {
<X> X fold(Supplier<X> empty, Function<A, X> just);
static <A, X> X empty0(Supplier<X> empty, Function<A, X> just) {
return empty.get();
}
static <A> Maybe<A> empty() {
return Maybe::empty0;
}
static void main(String[] args) {
Maybe<?> emptyString = Maybe.<String>empty();
Maybe<?> emptyInt = Maybe.<Integer>empty();
System.out.println(emptyString == emptyInt); // print "true".
}
}
我尝试将此编码移植到 scala 2.12,但它无法编译:
@FunctionalInterface
trait Maybe[A] {
def fold[X](empty: => X, just: A => X): X
}
object Maybe {
def empty0[A, X](empty: => X, just: A => X): X = empty
def empty[A]: Maybe[A] = empty0(_ ,_) // does not compile
def main(args: Array[String]): Unit = {
val emptyString: Maybe[String] = Maybe.empty
val emptyInt: Maybe[Integer] = Maybe.empty
print(emptyString eq emptyInt) // how to make this print "true"???
}
}
我得到的错误是:
missing parameter type for expanded function ((x: <error>, x: <error>) => empty0(x, x))
我的 objective 是让 scalac 触发与 Javac 相同的优化,使 java 程序打印 "true"。只要不使用 asInstanceOf
或 Nothing
/variance 注释,我愿意接受满足 scalac 所需的一切。
编辑:由于目前不支持,我为此开了一个feature request on the scala issue tracker(请投票!;-)。
很遗憾,the specification不允许这样做:
It follows that:
if class C defines a constructor, it must be accessible and must define exactly one, empty, argument list;
m cannot be polymorphic;
it must be possible to derive a fully-defined type U from S by inferring any unknown type parameters of C.
我的 objective 是通过新的 scala 2.12 支持 SAM(单一抽象方法)特征来实现代数数据类型(教会编码)的单例值。
在Java中,如下程序returntrue
:
import java.util.function.Function;
import java.util.function.Supplier;
@FunctionalInterface
public interface Maybe<A> {
<X> X fold(Supplier<X> empty, Function<A, X> just);
static <A, X> X empty0(Supplier<X> empty, Function<A, X> just) {
return empty.get();
}
static <A> Maybe<A> empty() {
return Maybe::empty0;
}
static void main(String[] args) {
Maybe<?> emptyString = Maybe.<String>empty();
Maybe<?> emptyInt = Maybe.<Integer>empty();
System.out.println(emptyString == emptyInt); // print "true".
}
}
我尝试将此编码移植到 scala 2.12,但它无法编译:
@FunctionalInterface
trait Maybe[A] {
def fold[X](empty: => X, just: A => X): X
}
object Maybe {
def empty0[A, X](empty: => X, just: A => X): X = empty
def empty[A]: Maybe[A] = empty0(_ ,_) // does not compile
def main(args: Array[String]): Unit = {
val emptyString: Maybe[String] = Maybe.empty
val emptyInt: Maybe[Integer] = Maybe.empty
print(emptyString eq emptyInt) // how to make this print "true"???
}
}
我得到的错误是:
missing parameter type for expanded function ((x: <error>, x: <error>) => empty0(x, x))
我的 objective 是让 scalac 触发与 Javac 相同的优化,使 java 程序打印 "true"。只要不使用 asInstanceOf
或 Nothing
/variance 注释,我愿意接受满足 scalac 所需的一切。
编辑:由于目前不支持,我为此开了一个feature request on the scala issue tracker(请投票!;-)。
很遗憾,the specification不允许这样做:
It follows that:
if class C defines a constructor, it must be accessible and must define exactly one, empty, argument list;
m cannot be polymorphic;
it must be possible to derive a fully-defined type U from S by inferring any unknown type parameters of C.