在scala中键入类型不等式的约束

时间:2012-08-27 01:19:37

标签: scala type-systems

  

可能重复:
  Enforce type difference

由于在scala =:=中存在强制执行相等性的通用类型约束,是否存在对类型强制执行“不等于”的约束?基本上!=但是对于类型?

修改

下面的评论指向现有的Q&A,答案似乎是(1)不,它不在标准库中(2)是的,可以定义一个。

所以我会修改我的问题,因为在看到答案后我发现了一个想法。

鉴于现有解决方案:

sealed class =!=[A,B]

trait LowerPriorityImplicits {
  implicit def equal[A]: =!=[A, A] = sys.error("should not be called")
}
object =!= extends LowerPriorityImplicits {
  implicit def nequal[A,B](implicit same: A =:= B = null): =!=[A,B] = 
    if (same != null) sys.error("should not be called explicitly with same type")
    else new =!=[A,B]
}     

case class Foo[A,B](a: A, b: B)(implicit e: A =!= B)

如果A <: BA >: BA =!= B仍然如此吗?如果没有,是否可以修改解决方案,以便A =!= B如果不是A <: BA >: B

1 个答案:

答案 0 :(得分:11)

shapeless使用与严格类型不等式相同的隐式歧义技巧来定义类型运算符A <:!< B(意思是A不是B的子类型)

trait <:!<[A, B]

implicit def nsub[A, B] : A <:!< B = new <:!<[A, B] {}
implicit def nsubAmbig1[A, B >: A] : A <:!< B = sys.error("Unexpected call")
implicit def nsubAmbig2[A, B >: A] : A <:!< B = sys.error("Unexpected call")

示例REPL会话,

scala> import shapeless.TypeOperators._
import shapeless.TypeOperators._

scala> implicitly[Int <:!< String]
res0: shapeless.TypeOperators.<:!<[Int,String] =
  shapeless.TypeOperators$$anon$2@200fde5c

scala> implicitly[Int <:!< Int]
<console>:11: error: ambiguous implicit values:
 both method nsubAmbig1 in object TypeOperators of type
   [A, B >: A]=> shapeless.TypeOperators.<:!<[A,B]
 and method nsubAmbig2 in object TypeOperators of type
   [A, B >: A]=> shapeless.TypeOperators.<:!<[A,B]
 match expected type shapeless.TypeOperators.<:!<[Int,Int]
              implicitly[Int <:!< Int]
                        ^

scala> class Foo ; class Bar extends Foo
defined class Foo
defined class Bar

scala> implicitly[Foo <:!< Bar]
res2: shapeless.TypeOperators.<:!<[Foo,Bar] =
  shapeless.TypeOperators$$anon$2@871f548

scala> implicitly[Bar <:!< Foo]
<console>:13: error: ambiguous implicit values:
 both method nsubAmbig1 in object TypeOperators of type
   [A, B >: A]=> shapeless.TypeOperators.<:!<[A,B]
 and method nsubAmbig2 in object TypeOperators of type
   [A, B >: A]=> shapeless.TypeOperators.<:!<[A,B]
 match expected type shapeless.TypeOperators.<:!<[Bar,Foo]
              implicitly[Bar <:!< Foo]
                        ^