Scala中类型不相等的证据 [英] Evidence that types are not equal in Scala

查看:50
本文介绍了Scala中类型不相等的证据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以约束方法,以便仅在证明两种类型相等时才有意义?

Is there any way to constraint a method so that it only makes sense if two types are proved not to be equal?

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)
}

推荐答案

是.来自不变形

// 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 = ???

这篇关于Scala中类型不相等的证据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆