Scala 映射 ->操作员

发布于 2024-10-17 01:40:22 字数 230 浏览 1 评论 0原文

Scala 中 Map 上下文中的符号 -> 有何含义?

Scala 的 Predef 类提供了一种隐式转换,可以让我们编写 键-> value 作为(key, value) 对的替代语法。 我在 ScalaByExample 中阅读了它,但没能看出这对于 Maps 是如何工作的。

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

Scala’s Predef class offers an implicit conversion that lets one write
key -> value as an alternate syntax for the pair (key, value).
I read it in ScalaByExample but fail to see how this works for Maps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不语却知心 2024-10-24 01:40:22

这是隐式转换:

implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] = new ArrowAssoc(x)

这会将任何类型转换为“ArrowAssoc”的实例,

class ArrowAssoc[A](x: A) {
    def -> [B](y: B): Tuple2[A, B] = Tuple2(x, y)
}

因此当 Scala 看到

"a"->1

它时,它会说“没有名为 '->' 的方法”在 String 上。范围内是否有任何隐式转换可以为我提供一个具有名为“->”的方法的类型?” Predef.scala 自动在范围内,并提供到 ArrowAssoc 的转换,它显然具有“->”方法。然后,Scala 将上面的内容转换为此

any2ArrowAssoc("a").->(1)

方法返回一个 Tuple2("a", 1)(通常称为 Pair)。 Map 有一个构造函数,它是 Tuple2 的数组(可变参数),所以我们开始比赛了!编译器中没有魔法(除了广泛用于许多不同目的的隐式转换),Maps 构造函数中也没有魔法。

Here's the implicit conversion:

implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] = new ArrowAssoc(x)

This will convert any type into an instance of "ArrowAssoc"

class ArrowAssoc[A](x: A) {
    def -> [B](y: B): Tuple2[A, B] = Tuple2(x, y)
}

So when Scala sees

"a"->1

It says "There is no method named '->' on String. Are there any implicit conversions in scope that can give me a type that has a method named '->'?" Predef.scala is automatically in scope, and offers a conversion to ArrowAssoc, which obviously has the '->' method. Scala then essentially converts the above to

any2ArrowAssoc("a").->(1)

This method returns a Tuple2("a", 1) (often called a Pair). Map has a constructor that thats an array (varargs) of Tuple2s, so we're off to races! No magic in the compiler (besides implicit conversion, which is used extensively and for many different purposes), and no magic in Maps constructor.

羅雙樹 2024-10-24 01:40:22

-> 运算符是在映射上下文中使用时将键与值关联起来的简写形式。在某些语言中,您只需将键和值对(通常键位于第一个位置,值位于第二个位置)传递给构造函数或映射上的各种方法之一,它将被处理适当地——Scala 映射也允许这样做。

但是,Scala 还提供了 key ->; 的简写形式。 value,正如您所发现的,它是通过隐式在 Predef 中定义的,以使关联更加清晰。本质上它是在说:“将左侧的项目映射到右侧的项目。”如果您阅读 Map 对象的定义,您会注意到它没有定义任何明显使用此 -> 方法的方法。相反,这些方法采用 Tuple2 类型的对象(这是一个包含 2 个值的元组:例如 (item1, item2)),Scala 通过 Predef 中的方法隐式转换该对象。

在某些语言中,-> 语法被定义为语言本身的一部分,但由于 Scala 的灵活性,可以将其简单地定义为方法。

The -> operator is a shorthand for associating a key with a value when used in the context of a map. In some languages, you are simply allowed to pass the pair of the key and value (usually with the key in the first position and the value in the second) to the constructor or one of the various methods on a map and it will be handled appropriately -- this is also allowed with Scala maps.

However, Scala also provides the shorthand form of key -> value, which, as you've discovered, is defined in Predef via an implicit, to make the association more clear. Essentially it's saying: "take the item on the left and map it to the item on the right." You'll notice if you read the definition of the Map object, that it does not define any methods that obviously use this -> method. Instead, the methods take objects of type Tuple2 (this is a tuple of 2 values: e.g. (item1, item2)), which Scala implicity converts via the method in Predef.

In some languages, the -> syntax is defined as part of the language itself, but due to the flexibility of Scala, this is able to be defined simply as a method.

话少情深 2024-10-24 01:40:22

-> 用于耦合映射的键和值。因此:

val m = Map(1 -> "one", 2 -> "two", 3 -> "three")

将把前 3 个正整数映射为等价文本(即 m(1) 将是 "one" 等)。您也可以将其写为,

val m = Map((1,"one"), (2,"two"), (3,"three"))

但第一种方式看起来更好,这就是为什么它被提供作为替代方案。

-> is used to couple keys and values for a map. So:

val m = Map(1 -> "one", 2 -> "two", 3 -> "three")

will map the first 3 positive integers into text equivalents (that is, m(1) will be "one", etc.). You could also write it as

val m = Map((1,"one"), (2,"two"), (3,"three"))

but the first way looks nicer, which is why it's provided as an alternative.

厌倦 2024-10-24 01:40:22

正如 Adam 所说,-> 不是 scala 语法,它是 ArrowAssoc 类的方法。当你写“foo”时 -> “bar”时,编译器插入从“foo”到 ArrowAssoc 实例的隐式转换,以便可以找到 -> 方法。

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)
      ...
}

谷歌““scala rich 包装器”了解更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文