Scala 使用implicits对抽象类型成员进行约束的trait的Restrict方法?
我的情况如下:Scala 使用implicits对抽象类型成员进行约束的trait的Restrict方法?,scala,type-conversion,scalaz,implicits,Scala,Type Conversion,Scalaz,Implicits,我的情况如下: import scalaz.Leibniz._ trait Exp[T, C] { def &&(that: Exp[T, C])(implicit evT: T === Boolean) = LogicalAnd(this, that) def &&(that: Exp[T, C])(implicit evT: T === Int) = BitwiseAnd(this, that) } case class LogicalAn
import scalaz.Leibniz._
trait Exp[T, C] {
def &&(that: Exp[T, C])(implicit evT: T === Boolean) = LogicalAnd(this, that)
def &&(that: Exp[T, C])(implicit evT: T === Int) = BitwiseAnd(this, that)
}
case class LogicalAnd[C](e1: Exp[Boolean, C], e2: Exp[Boolean, C]) extends Exp[Boolean, C]
case class LogicalOr[C](e1: Exp[Boolean, C], e2: Exp[Boolean, C]) extends Exp[Boolean, C]
...
case class BitwiseAnd[C](e1: Exp[Int, C], e2: Exp[Int, C]) extends Exp[Int, C]
case class BitwiseOr[C](e1: Exp[Int, C], e2: Exp[Int, C]) extends Exp[Int, C]
...
trait Exp[T,C]是DSL的基本trait和AST,我想在这个trait中重载内置的scala操作符,以允许在这个DSL上使用中缀符号,然而,我想在trait级别用类型T上的一个绑定来约束这些方法中的一些,以便这里的相同操作“&&”根据类型T具有不同的语义
似乎莱布尼兹替代在这里不起作用(可能是因为它只为带有一个参数的函子F[1;]定义):
这种限制特质T参数的方法有意义吗?
在这种情况下,有没有一种方法可以让莱布尼茨通过“隐藏”第二个参数C来工作,比如:
type ExpF[T] = Exp[T, _]
这有意义吗?
谢谢,就是这样;定义这样一种类型,然后使用
Leibniz.lift
来提升你的Leibniz
:
def &&(that: Exp[T, C])(implicit evT: T === Boolean) = {
type ExpF[A] = Exp[A, C]
val ev2: (Exp[T, C] === Exp[Boolean, C]) =
Leibniz.lift[⊥, ⊥, ⊤, ⊤, ExpF, T, Boolean](evT)
LogicalAnd(this, that)
}
def &&(that: Exp[T, C])(implicit evT: T === Boolean) = {
type ExpF[A] = Exp[A, C]
val ev2: (Exp[T, C] === Exp[Boolean, C]) =
Leibniz.lift[⊥, ⊥, ⊤, ⊤, ExpF, T, Boolean](evT)
LogicalAnd(this, that)
}