我的目标是通过新的scala 2.12支持SAM(单一抽象方法)特征来实现代数数据类型(教会编码)的单例值.
在Java中,以下程序返回true:
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"???
}
}
我得到的错误是:
扩展函数缺少参数类型((x $1:<错误>,x $2:<错误>)=> empty0(x $1,x $2))
我的目标是让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.
原文地址:https://codeday.me/bug/20190627/1310043.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。