I am new to scala and I wrote this in repl
val xx = Array.ofDim [String](3,4)
xx: Array[Array[String]] = Array(Array(null, null, null, null), Array(null, null,
null, null), Array(null, null, null, null))
@ val yy = Array.ofDim [Int](3,4)
yy: Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0))
@ val ss = Array(xx, yy)
which resulted in this
ss: Array[Array[_1] forSome { type _1 >: Array[Int] with Array[String] <: Array[_1] forSome { type _1 >: Int with String } }] = Array(
Array(Array(null, null, null, null), Array(null, null, null, null), Array(null, null, null, null)),
Array(Array(0, 0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0))
)
can someone please explain what does this mean
Array[_1] forSome { type _1 >: Array[Int] with Array[String] <: Array[_1] forSome { type _1 >: Int with String } }
specially the >: .....<: .... part. by the way I am using scala 2.13.
cheers,
es
CodePudding user response:
The forSome keyword in Scala creates an existential type. That is a type that you don't know or don't care what it precisely is, you can just provide conditions for it. More in this answer.
The <: and :> operators mean type bound. A definition like type A <: B means, that type A is a subtype of B. And the definition type A >: B means that A is a supertype of B.
In this case (last snippet), it means that you have an Array with a parameter _1 about which we know that it is a supertype of Array[Int] with Array[String] and a subtype of Array[_1] forSome { type _1 >: Int with String }
