Scala maps -> operator

后端 未结 4 1131
离开以前
离开以前 2020-12-23 16:54

What does the symbol -> mean in the context of a Map in Scala?

Scala’s Predef class offers an implicit conversion that lets

4条回答
  •  误落风尘
    2020-12-23 17:16

    As Adam said, -> is not scala syntax, it is a method of the class ArrowAssoc. When you write "foo" -> "bar", the compiler inserts a implicit conversion from "foo" to ArrowAssoc instance so that the -> method can be found.

    package scala
    object Predef {
       class ArrowAssoc[A](x: A) {
           def -> [B](y: B): Tuple2[A, B] = Tuple2(x, y)
          }
          implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] =
            new ArrowAssoc(x)
          ...
    }
    

    google "“scala rich wrappers” to know more.

提交回复
热议问题