ない答えが、REPLに暗黙的を使用していくつかの手がかり。コンパイラは型が同じだとは思わない。推論されたタイプは、より具体的である:
// some type aliases to make reading easier
type Dx = java.lang.Double
type Ix = java.lang.Integer
// the type the compiler came up with:
type Inferred = Number with Comparable[
_ >: Dx with Ix <: Number with Comparable[_ >: Dx with Ix <: Number]]
// your type:
type Soc = Number with Comparable[_ >: Dx with Ix <: Number]
私は右のタイプの別名をしたチェック:
val d = new java.lang.Double(4)
val i = new java.lang.Integer(4)
val foo: Soc = if (true) d else i
// foo: Soc = 4.0
val foo: Inferred = if (true) d else i
// foo: Inferred = 4.0
種類は同じではありません。
implicitly[Soc =:= Inferred] // error
あなたのタイプはのスーパータイプであります推定タイプ:
implicitly[Inferred <:< Soc] // ok
implicitly[Soc <:< Inferred] // error
コンパイラによれば、より具体的な型が出てきました。これは正しいことです。ユースケースは、次のように再作成することができることに注意してください:
class N // like java.lang.Number
trait C[T] // like Comparable
class I extends N with C[I] // like java.lang.Integer
class D extends N with C[D] // like java.lang.Double
type DI = N with C[_ >: D with I <: N with C[_ >: D with I <: N]]
// DI is like the type inferred
type DI_SOC = N with C[_ >: D with I <: N] // your type
val foo: DI = if (true) new D else new I // ok
val foo: DI_SOC = if (true) new D else new I // ok
implicitly[DI =:= DI_SOC] // error
implicitly[DI <:< DI_SOC] // DI_SOC super type of DI
implicitly[DI_SOC <:< DI] // error
をだから私たちはDI
とDI_SOC
が同じタイプではない説明ですDI_SOC
ではなくDI
あるクラスを、作ることができるかどうかだろうとあなたのタイプは最小の上限ではありません。
[OK]をクリックしてコンピュータを少しステップしてからもう一度やり直してください。
class A extends N with C[N]
implicitly[A <:< DI_SOC] // ok
implicitly[A <:< DI] // error
元のユースケースに適用される:
class Ax extends Number with Comparable[Number] {
def doubleValue() = 0d
def floatValue() = 0f
def intValue() = 0
def longValue() = 0L
def compareTo(n: Number) = 0
}
implicitly[Ax <:< Soc] // ok
implicitly[Ax <:< Inferred] // error
したがって、タイプSoc
とInferred
がない同じであり、Ax
が証明ここでDI_SOC
なくDI
あるクラスがありますそのNumber with Comparable[_ >: Double with Integer <: Number]
はではなく、最小上限...
つまり、Double with Integer <: ? <: Number
の間に若干の余裕がありますが、それほど多くはありませんDouble with Integer <: ? <: Number with Comparable[_ >: Double with Integer <: Number]