Scala 映射 ->操作员
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 writekey -> 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是隐式转换:
这会将任何类型转换为“ArrowAssoc”的实例,
因此当 Scala 看到
它时,它会说“没有名为 '->' 的方法”在 String 上。范围内是否有任何隐式转换可以为我提供一个具有名为“->”的方法的类型?” Predef.scala 自动在范围内,并提供到 ArrowAssoc 的转换,它显然具有“->”方法。然后,Scala 将上面的内容转换为此
方法返回一个 Tuple2("a", 1)(通常称为 Pair)。 Map 有一个构造函数,它是 Tuple2 的数组(可变参数),所以我们开始比赛了!编译器中没有魔法(除了广泛用于许多不同目的的隐式转换),Maps 构造函数中也没有魔法。
Here's the implicit conversion:
This will convert any type into an instance of "ArrowAssoc"
So when Scala sees
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
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.
->
运算符是在映射上下文中使用时将键与值关联起来的简写形式。在某些语言中,您只需将键和值对(通常键位于第一个位置,值位于第二个位置)传递给构造函数或映射上的各种方法之一,它将被处理适当地——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 typeTuple2
(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.->
用于耦合映射的键和值。因此:将把前 3 个正整数映射为等价文本(即
m(1)
将是"one"
等)。您也可以将其写为,但第一种方式看起来更好,这就是为什么它被提供作为替代方案。
->
is used to couple keys and values for a map. So:will map the first 3 positive integers into text equivalents (that is,
m(1)
will be"one"
, etc.). You could also write it asbut the first way looks nicer, which is why it's provided as an alternative.
正如 Adam 所说,
->
不是 scala 语法,它是ArrowAssoc
类的方法。当你写“foo”时 -> “bar”时,编译器插入从“foo”到 ArrowAssoc 实例的隐式转换,以便可以找到->
方法。谷歌““scala rich 包装器”了解更多信息。
As Adam said,
->
is not scala syntax, it is a method of the classArrowAssoc
. When you write "foo" -> "bar", the compiler inserts a implicit conversion from "foo" to ArrowAssoc instance so that the->
method can be found.google "“scala rich wrappers” to know more.