经过2.13的测试,但我想从2.12开始就是这样,而我之前从未遇到过此类问题:
trait Extractor[-X, +Y] {
def optional :X => Option[Y] = apply
def apply(x :X) :Option[Y]
def andThen[Z](extractor :Extractor[Y, Z]) :Extractor[X, Z] = {
val first = optional; val second = extractor.optional
Extractor { x :X => first(x).flatMap(second) }
}
def andThen[Z](req :Y => Z) :Extractor[X, Z] = {
val first = optional
Extractor { x :X => first(x).map(req) }
}
def compose[W](extractor :Extractor[W, X]) :Extractor[W, Y] = extractor andThen this
def compose[W](req :W => X) :Extractor[W, Y] = Extractor(req andThen optional)
}
Scalac对第一个实施方案有一个神秘的抱怨compose
:
Error:(44, 31) type mismatch;
found : net.noresttherein.oldsql.morsels.Extractor[X,Y]
required: W => ?
def compose[W](extractor :Extractor[W, X]) :Extractor[W, Y] = extractor andThen this
注释掉andThen
带有函数的变量可以解决此问题。更改代码也是如此extractor.andThen[Y](this)
(此处明确类型参数是关键)。我的猜测是我的SAM类型以某种方式提升为函数,但是我无法猜测为什么它会优先于方法。正如我在这里提供的两个“解决方案”一样,我不是很想办法,而是要了解正在发生的事情。这是多年来我第一次不知道发生了什么,并希望将来消除类似的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句