3

When I read Mxnet source code, I was confused in following statements:

object NDArray {
  private val logger = LoggerFactory.getLogger(classOf[NDArray])
  private[mxnet] val DTYPE_NATIVE_TO_MX: Map[Class[_ >: Float with Int with Double], Int] = Map(
    classOf[Float] -> 0,
    classOf[Double] -> 1,
    classOf[Int] -> 4
  )

What does it mean for "Class[_ >: Float with Int with Double], Int]"? I understand the scala keyword "with" could be used during class declaration, for example

Class person with glass { 

means the class 'person' has the trait of objdect 'glass'.

How to interpret the usage of 'with' in above code?

Leopd
  • 41,333
  • 31
  • 129
  • 167
David Wang
  • 31
  • 1

1 Answers1

2

The with keyword is used for expressing intersection types.

The type Float with Int with Double is basically a subtype of Float and Int and Double. Of course you cannot have an actual value of this type because Float, Int and Double are all final classes. Here, in the type Map[Class[_ >: Float with Int with Double], Int], it is used to express that every key of the Map has to be a Class[T] where T has to be a supertype of Float with Int with Double. And those supertypes are Float, Int and Double (and AnyVal and Any, if we go higher up the inheritance chain).

Jasper-M
  • 14,966
  • 2
  • 26
  • 37