了解多维数组创建期间的参数类型
Understanding parameter types during multidimensional array creation
我正在尝试解析在以下代码中创建的参数类型 type _1 >: Char with Int <: AnyVal
的含义(使用 Scala ammonite repl)。我正在定义一个数组数组
- 在这种情况下,
with
是什么意思,我们是否用 Int 的 'traits' 定义了一个新的 Char class?
- 表达式
>: Char with Int <: AnyVal
是否表示 - 任何类型 (_1) 是具有 Int 的 Char 的超类型但是 AnyVal 的子类型?
@ val tmix1 = Array(Array[Int](1,2,3), Array[Char]('b','f'))
tmix1: Array[Array[_1] forSome { type _1 >: Char with Int <: AnyVal }]
= Array(Array(1, 2, 3), Array('b', 'f'))
Does the expression >: Char with Int <: AnyVal mean - Any type (_1)
that is a supertype of Char with Int but a subtype of AnyVal?
是的,这是正确的。
are we defining a new Char class with the 'traits' of Int
我们没有定义任何东西。
你在写 'x: Int => String' 时定义了 'class' 吗?不,它只是类型表达式。
这是一种允许您表达类型的语法。使用关键字 'with' 可以构造一个类型,该类型是 Char 的子类型和 Int 的子类型。该表达式可以用作联合类型编码 ()。联合类型是一种通过逻辑“或”将几种类型组合成单一类型的类型。它也称为“类型析取”。凭直觉,您可以通过想象示例来尝试理解这一点:
// what is the possible supertypes for type 'Char with Int'?
// it is 'Char with Int', Int, Char, AnyVal, Any
val x: Char with Int = Char with Int
val a: Int = Char with Int
val b: Char = Char with Int
val c: AnyVal = Char with Int
val d: An y= Char with Int
所以T >: Char with Int
和T <: AnyVal
几乎等同于Char Or Int
关于 Char Or Int
的直觉,我可以给你这个:
val a: Char Or Int = 1 // is it correct? yes it is
val b: Char Or Int = 'a' // it is correct too
// after that, you can pattern match on such type in a safe manner
def foo(x: Char Or Int) match {
case Int => ...
case Char => ...
}
所以。
你的第二个问题是正确的。
你在第一个不太正确,因为它不是 'class' 或 'trait' - 它是类型表达式。这不直观,因为你有相同的词 'with' 就像在 类 和特征的继承中一样。但是这个词在不同的语境下意思有点不同。
这种构造是表达联合类型的方式,非常适合原始表达式的类型val tmix1 = Array(Array[Int](1,2,3), Array[Char]('b','f'))
我正在尝试解析在以下代码中创建的参数类型 type _1 >: Char with Int <: AnyVal
的含义(使用 Scala ammonite repl)。我正在定义一个数组数组
- 在这种情况下,
with
是什么意思,我们是否用 Int 的 'traits' 定义了一个新的 Char class? - 表达式
>: Char with Int <: AnyVal
是否表示 - 任何类型 (_1) 是具有 Int 的 Char 的超类型但是 AnyVal 的子类型?
@ val tmix1 = Array(Array[Int](1,2,3), Array[Char]('b','f'))
tmix1: Array[Array[_1] forSome { type _1 >: Char with Int <: AnyVal }]
= Array(Array(1, 2, 3), Array('b', 'f'))
Does the expression >: Char with Int <: AnyVal mean - Any type (_1) that is a supertype of Char with Int but a subtype of AnyVal?
是的,这是正确的。
are we defining a new Char class with the 'traits' of Int
我们没有定义任何东西。 你在写 'x: Int => String' 时定义了 'class' 吗?不,它只是类型表达式。 这是一种允许您表达类型的语法。使用关键字 'with' 可以构造一个类型,该类型是 Char 的子类型和 Int 的子类型。该表达式可以用作联合类型编码 ()。联合类型是一种通过逻辑“或”将几种类型组合成单一类型的类型。它也称为“类型析取”。凭直觉,您可以通过想象示例来尝试理解这一点:
// what is the possible supertypes for type 'Char with Int'?
// it is 'Char with Int', Int, Char, AnyVal, Any
val x: Char with Int = Char with Int
val a: Int = Char with Int
val b: Char = Char with Int
val c: AnyVal = Char with Int
val d: An y= Char with Int
所以T >: Char with Int
和T <: AnyVal
几乎等同于Char Or Int
关于 Char Or Int
的直觉,我可以给你这个:
val a: Char Or Int = 1 // is it correct? yes it is
val b: Char Or Int = 'a' // it is correct too
// after that, you can pattern match on such type in a safe manner
def foo(x: Char Or Int) match {
case Int => ...
case Char => ...
}
所以。
你的第二个问题是正确的。
你在第一个不太正确,因为它不是 'class' 或 'trait' - 它是类型表达式。这不直观,因为你有相同的词 'with' 就像在 类 和特征的继承中一样。但是这个词在不同的语境下意思有点不同。
这种构造是表达联合类型的方式,非常适合原始表达式的类型val tmix1 = Array(Array[Int](1,2,3), Array[Char]('b','f'))