Scala中类型不相等的证据

Hug*_*ira 4 types scala implicits shapeless

有没有办法约束一个方法,只有两种类型被证明相等才有意义?

trait Something[A, B] {
  // I can only be called if type A is the same as type B
  def ifEqual(implicit ev: A =:= B)

  // Now I cannot be called if type A is proven to be the same as type B
  def ifNotEqual(implicit ev: A ??? B)
}
Run Code Online (Sandbox Code Playgroud)

Mil*_*bin 12

是.从无形,

// Type inequalities
trait =:!=[A, B] 

implicit def neq[A, B] : A =:!= B = new =:!=[A, B] {}
implicit def neqAmbig1[A] : A =:!= A = ???
implicit def neqAmbig2[A] : A =:!= A = ???
Run Code Online (Sandbox Code Playgroud)

  • 这必须是重复的,除了它很酷,迈尔斯介入回答它.当马歇尔·麦克卢汉(Marshall McLuhan)出现在电影界时,就像安妮·霍尔(Annie Hall)那样."如果生活只是这样!" (3认同)