我认为第一类不相交类型是一个密封的超类型,具有备用子类型,以及与这些替代子类型的所需分离类型的隐式转换。
我假设这个地址comments 33 - Miles Sabin 的解决方案的 36 个,所以是可以在使用现场使用的第一类类型,但我没有测试它。
sealed trait IntOrString
case class IntOfIntOrString( v:Int ) extends IntOrString
case class StringOfIntOrString( v:String ) extends IntOrString
implicit def IntToIntOfIntOrString( v:Int ) = new IntOfIntOrString(v)
implicit def StringToStringOfIntOrString( v:String ) = new StringOfIntOrString(v)
object Int {
def unapply( t : IntOrString ) : Option[Int] = t match {
case v : IntOfIntOrString => Some( v.v )
case _ => None
}
}
object String {
def unapply( t : IntOrString ) : Option[String] = t match {
case v : StringOfIntOrString => Some( v.v )
case _ => None
}
}
def size( t : IntOrString ) = t match {
case Int(i) => i
case String(s) => s.length
}
scala> size("test")
res0: Int = 4
scala> size(2)
res1: Int = 2
一个问题是 Scala 不会在匹配上下文的情况下使用从 IntOfIntOrString 到 Int 的隐式转换(以及从 StringOfIntOrString 到 String),因此必须定义提取器并使用 case Int(i) 而不是 @987654344 @。
添加:我在他的博客中对 Miles Sabin 的回复如下。或许对 Either 有一些改进:
- 它扩展到2种以上,在使用或定义现场没有任何额外的噪音。
- 参数被隐式装箱,例如不需要
size(Left(2)) 或size(Right("test"))。
- 模式匹配的语法被隐式拆箱。
- 装箱和拆箱可能会被 JVM 热点优化掉。
- 该语法可能是未来一流联合类型采用的语法,因此迁移可能是无缝的?也许对于联合类型名称,最好使用
V 而不是Or,例如IntVString、`Int |v| String`、`Int or String`,还是我最喜欢的`Int|String`?
更新:上述模式的析取逻辑否定如下,我added an alternative (and probably more useful) pattern at Miles Sabin's blog。
sealed trait `Int or String`
sealed trait `not an Int or String`
sealed trait `Int|String`[T,E]
case class `IntOf(Int|String)`( v:Int ) extends `Int|String`[Int,`Int or String`]
case class `StringOf(Int|String)`( v:String ) extends `Int|String`[String,`Int or String`]
case class `NotAn(Int|String)`[T]( v:T ) extends `Int|String`[T,`not an Int or String`]
implicit def `IntTo(IntOf(Int|String))`( v:Int ) = new `IntOf(Int|String)`(v)
implicit def `StringTo(StringOf(Int|String))`( v:String ) = new `StringOf(Int|String)`(v)
implicit def `AnyTo(NotAn(Int|String))`[T]( v:T ) = new `NotAn(Int|String)`[T](v)
def disjunction[T,E](x: `Int|String`[T,E])(implicit ev: E =:= `Int or String`) = x
def negationOfDisjunction[T,E](x: `Int|String`[T,E])(implicit ev: E =:= `not an Int or String`) = x
scala> disjunction(5)
res0: Int|String[Int,Int or String] = IntOf(Int|String)(5)
scala> disjunction("")
res1: Int|String[String,Int or String] = StringOf(Int|String)()
scala> disjunction(5.0)
error: could not find implicit value for parameter ev: =:=[not an Int or String,Int or String]
disjunction(5.0)
^
scala> negationOfDisjunction(5)
error: could not find implicit value for parameter ev: =:=[Int or String,not an Int or String]
negationOfDisjunction(5)
^
scala> negationOfDisjunction("")
error: could not find implicit value for parameter ev: =:=[Int or String,not an Int or String]
negationOfDisjunction("")
^
scala> negationOfDisjunction(5.0)
res5: Int|String[Double,not an Int or String] = NotAn(Int|String)(5.0)
另一个更新:关于Mile Sabin's solution 的 cmets 23 和 35,这里有一种在使用站点声明联合类型的方法。请注意,它在第一级之后被取消装箱,即它的优势是extensible to any number of types in the disjunction,而Either 需要嵌套装箱,并且我之前的评论 41 中的范例是不可扩展的。换句话说,D[Int ∨ String] 可以分配给D[Int ∨ String ∨ Double](即是其子类型)。
type ¬[A] = (() => A) => A
type ∨[T, U] = ¬[T] with ¬[U]
class D[-A](v: A) {
def get[T](f: (() => T)) = v match {
case x : ¬[T] => x(f)
}
}
def size(t: D[Int ∨ String]) = t match {
case x: D[¬[Int]] => x.get( () => 0 )
case x: D[¬[String]] => x.get( () => "" )
case x: D[¬[Double]] => x.get( () => 0.0 )
}
implicit def neg[A](x: A) = new D[¬[A]]( (f: (() => A)) => x )
scala> size(5)
res0: Any = 5
scala> size("")
error: type mismatch;
found : java.lang.String("")
required: D[?[Int,String]]
size("")
^
scala> size("hi" : D[¬[String]])
res2: Any = hi
scala> size(5.0 : D[¬[Double]])
error: type mismatch;
found : D[(() => Double) => Double]
required: D[?[Int,String]]
size(5.0 : D[?[Double]])
^
显然 Scala 编译器存在三个错误。
- 它不会为目标析取中第一个类型之后的任何类型选择正确的隐式函数。
- 它不会从匹配中排除
D[¬[Double]] 大小写。
3.
scala> class D[-A](v: A) {
def get[T](f: (() => T))(implicit e: A <:< ¬[T]) = v match {
case x : ¬[T] => x(f)
}
}
error: contravariant type A occurs in covariant position in
type <:<[A,(() => T) => T] of value e
def get[T](f: (() => T))(implicit e: A <:< ?[T]) = v match {
^
get 方法没有正确限制输入类型,因为编译器不允许A 在协变位置。有人可能会争辩说这是一个错误,因为我们想要的只是证据,我们永远不会访问函数中的证据。我选择不在get 方法中测试case _,所以我不必在match 中的size() 中拆箱Option。
2012 年 3 月 5 日:之前的更新需要改进。 Miles Sabin's solution 可以正确使用子类型。
type ¬[A] = A => Nothing
type ∨[T, U] = ¬[T] with ¬[U]
class Super
class Sub extends Super
scala> implicitly[(Super ∨ String) <:< ¬[Super]]
res0: <:<[?[Super,String],(Super) => Nothing] =
scala> implicitly[(Super ∨ String) <:< ¬[Sub]]
res2: <:<[?[Super,String],(Sub) => Nothing] =
scala> implicitly[(Super ∨ String) <:< ¬[Any]]
error: could not find implicit value for parameter
e: <:<[?[Super,String],(Any) => Nothing]
implicitly[(Super ? String) <:< ?[Any]]
^
我之前的更新提案(针对接近一流的联合类型)打破了子类型化。
scala> implicitly[D[¬[Sub]] <:< D[(Super ∨ String)]]
error: could not find implicit value for parameter
e: <:<[D[(() => Sub) => Sub],D[?[Super,String]]]
implicitly[D[?[Sub]] <:< D[(Super ? String)]]
^
问题是(() => A) => A中的A同时出现在协变(返回类型)和逆变(函数输入,或者在这种情况下是作为函数输入的函数的返回值)位置,因此替换只能保持不变。
请注意,A => Nothing 是必需的,因为我们希望 A 处于逆变位置,因此 A 的超类型 are not subtypes 的 D[¬[A]] 和 D[¬[A] with ¬[U]] (see also)。由于我们只需要双重逆变,即使我们可以丢弃¬和∨,我们也可以实现等效于Miles的解决方案。
trait D[-A]
scala> implicitly[D[D[Super]] <:< D[D[Super] with D[String]]]
res0: <:<[D[D[Super]],D[D[Super] with D[String]]] =
scala> implicitly[D[D[Sub]] <:< D[D[Super] with D[String]]]
res1: <:<[D[D[Sub]],D[D[Super] with D[String]]] =
scala> implicitly[D[D[Any]] <:< D[D[Super] with D[String]]]
error: could not find implicit value for parameter
e: <:<[D[D[Any]],D[D[Super] with D[String]]]
implicitly[D[D[Any]] <:< D[D[Super] with D[String]]]
^
所以完整的修复是。
class D[-A] (v: A) {
def get[T <: A] = v match {
case x: T => x
}
}
implicit def neg[A](x: A) = new D[D[A]]( new D[A](x) )
def size(t: D[D[Int] with D[String]]) = t match {
case x: D[D[Int]] => x.get[D[Int]].get[Int]
case x: D[D[String]] => x.get[D[String]].get[String]
case x: D[D[Double]] => x.get[D[Double]].get[Double]
}
请注意 Scala 中的前 2 个错误仍然存在,但第 3 个错误已被避免,因为 T 现在被限制为 A 的子类型。
我们可以确认子类型的工作。
def size(t: D[D[Super] with D[String]]) = t match {
case x: D[D[Super]] => x.get[D[Super]].get[Super]
case x: D[D[String]] => x.get[D[String]].get[String]
}
scala> size( new Super )
res7: Any = Super@1272e52
scala> size( new Sub )
res8: Any = Sub@1d941d7
我一直认为一流的交集类型非常重要,无论是对于reasons Ceylon has them,而且因为不是subsuming 到Any 这意味着在预期类型上使用match 取消装箱可以生成运行时错误,(heterogeneous collection 包含一个)析取的拆箱可以进行类型检查(Scala 必须修复我注意到的错误)。对于异构集合,联合比 complexity of using 和 metascala 的实验性 HList 更直接。