2

这是一个例子:

sealed trait Type
object Type {
    case object Type1 extends Type
    case object Type2 extends Type
}

sealed trait TypedTrait {
    type Tpe <: Type
}

object TypedTrait {
    type Aux[T <: Type] = TypedTrait{ type Tpe = T }
}

sealed trait Test {
    //Bounded with TypedTrait.Aux[Type.Type1.type]
    type TptTrait <: TypedTrait.Aux[Type.Type1.type]
}

object Test {
    //No TypedTrait.Aux[Type.Type1.type] type bound
    type Aux[T <: TypedTrait] = Test { type TptTrait = T } //compiles fine, but I expected error
}

该示例为Scala 2Scala 3编译,但似乎没有意义。为什么会编译?

4

1 回答 1

4

虽然这样编译你不能用坏类型实例化这个类型:

class Wololo extends TypedTrait { type Tpe = Type.Type2.type } // bad type
val ax: Test.Aux[Wololo] = new Test { type TptTrait = Wololo }

这失败了

cmd1.sc:24: incompatible type in overriding
type TptTrait <: ammonite.$sess.cmd1.TypedTrait.Aux[ammonite.$sess.cmd1.Type.Type1.type] (defined in trait Test);
 found   : ammonite.$sess.cmd1.Wololo
 required:  <: ammonite.$sess.cmd1.TypedTrait.Aux[ammonite.$sess.cmd1.Type.Type1.type]
    (which expands to)   <: ammonite.$sess.cmd1.TypedTrait{type Tpe = ammonite.$sess.cmd1.Type.Type1.type}
val ax: Test.Aux[Wololo] = new Test { type TptTrait = Wololo }
                                           ^
Compilation Failed

同时

class Wololo extends TypedTrait { type Tpe = Type.Type1.type } // good type
val ax: Test.Aux[Wololo] = new Test { type TptTrait = Wololo }

成功

ax: Test.Aux[Wololo] = ammonite.$sess.cmd1$$anon$1@78de58ea

所以我们可以得出结论,这里的边界结合了,但在您实际尝试创建一个违反约束的实例之前不会失败。可能这不会触发规范让它立即失败的任何情况(毕竟你可以创建一个有效的实例!),所以检查/证明被推迟到你尝试创建一个实际的实例。

sealed trait(因为s ,我在 Ammonite 中将其作为一个整体进行了评估)

@ {
  sealed trait Type
  object Type {
      case object Type1 extends Type
      case object Type2 extends Type
  }

  sealed trait TypedTrait {
      type Tpe <: Type
  }

  object TypedTrait {
      type Aux[T <: Type] = TypedTrait{ type Tpe = T }
  }

  sealed trait Test {
      //Bounded with TypedTrait.Aux[Type.Type1.type]
      type TptTrait <: TypedTrait.Aux[Type.Type1.type]
  }

  object Test {
      type Aux[T <: TypedTrait] = Test { type TptTrait = T }
  }
  class Wololo extends TypedTrait { type Tpe = Type.Type1.type }
  val ax: Test.Aux[Wololo] = new Test { type TptTrait = Wololo }
  }
defined trait Type
defined object Type
defined trait TypedTrait
defined object TypedTrait
defined trait Test
defined object Test
defined class Wololo
ax: Test.Aux[Wololo] = ammonite.$sess.cmd1$$anon$1@78de58ea
于 2021-01-14T15:43:46.157 回答