本文介绍了scala - Mixin 类型约束仍然可以编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我注意到这个可以编译,但不知道为什么.
I've noticed this compiles and am not sure why.
trait NotFuture {
type Out[+T]
implicitly[Out[_] =!= scala.concurrent.Future[_]]
}
val newNotFuture = new NotFuture {
type Out[+T] = scala.concurrent.Future[T]
}
知道如何进行这项工作吗?
Any idea how to make this work?
推荐答案
不幸的是,在 Out
被实际定义之前,我认为不可能构建所需的不等式证明.我能得到的最接近你想要的东西是这样的,
Unfortunately I don't think it's possible to construct the required inequality proof until Out
is actually defined. The closest I can get to what you want is something like this,
scala> import shapeless._ // for =:!=
import shapeless._
scala> import scala.concurrent.Future
import scala.concurrent.Future
scala> trait NotFuture {
| type Out[+T]
| val ev: Out[_] =:!= Future[_]
| def prf(implicit ev: Out[_] =:!= Future[_]) = ev
| }
defined trait NotFuture
scala> val nf = new NotFuture { type Out[+T] = List[T] ; val ev = prf }
nf: NotFuture{type Out[+T] = List[T]} = $anon$1@5723cc36
scala> val nf = new NotFuture { type Out[+T] = Future[T] ; val ev = prf }
<console>:12: error: ambiguous implicit values:
both method neqAmbig1 in package shapeless of type [A]=> shapeless.=:!=[A,A]
and method neqAmbig2 in package shapeless of type [A]=> shapeless.=:!=[A,A]
match expected type shapeless.=:!=[this.Out[_],scala.concurrent.Future[_]]
val nf = new NotFuture { type Out[+T] = Future[T] ; val ev = prf }
请注意,提供证明是强制性的(因为 ev
在 NotFuture
中是抽象的)并且在子类中半隐式提供(val ev = prf代码>).
Notice that providing the proof is mandatory (because ev
is abstract in NotFuture
) and provided semi-implicitly in the subclasses (val ev = prf
).
这篇关于scala - Mixin 类型约束仍然可以编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!