Scala 和 :: 对象 [英] Scala and the :: object

查看:34
本文介绍了Scala 和 :: 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在潜入 scala,到目前为止我非常喜欢它.我正在研究模式匹配和案例类,下面的内容让我有些困惑.我知道它的作用,但我想确切地了解发生了什么

I've been diving into scala, and am absolutely loving it so far. I'm getting around to pattern matching and case classes, and the following has me somewhat stumped. I know what it does, but I want to understand exactly what is happening

var list = List(2,3,4)
1::list

如果我到目前为止的理解是正确的.然后 :: 代表一个 case 对象.如果是这样,我的问题是:

If I am correct in my understanding so far. Then the :: represents a case object. If so my question is:

我如何将其应用"到 1 上?而不是 :: 是 1 的方法.基本上有人可以将这个语句 1::list 分开一些,显示真正发生的事情(即在什么对象上调用什么方法)

how am I "left applying" it to the 1? instead of :: being a method of 1. Basically can someone pull this statement 1::list apart somewhat, showing what is really happening (ie. what methods are being called on what object)

谢谢

推荐答案

令人恼火的是,Scala 的一些最明显和最棒的功能在表面之下却如此复杂.因此,请考虑以下简单的行:

It's annoying that some of the most visible and awesome features of Scala have so much complexity right beneath the surface. So, consider this simple line:

val (head :: tail): ::[Int] = 1 :: Nil

:: 出现的三个地方中的每一个都指代不同的 :: 和 Scala 中的不同机制.让我们按顺序逐一介绍.

Each of the three places where :: appears refers to a different ::, and a different mechanism in Scala. Let's go through each of them, in order.

这里发生的是模式匹配,就像人们看到的case语句一样.模式匹配可以出现在 val 赋值、<- 的左侧,在 for 推导式中,以及 case> 声明.

What is happening here is pattern matching, just like one sees with case statements. Pattern matching can appear on val assignments, on the left side of <- in for comprehensions, and on case statements.

那么,这种特殊的模式匹配是如何发生的?好吧,每当模式采用 abc 格式时,Scala 会将其转换为 b(a, c),然后将其转换为对 unapplyunapplySeq 在对象 b 上.

So, how does this particular pattern matching happens? Well, whenever the pattern is in the format a b c, Scala translates this into b(a, c), which is then translated into calls to unapply or unapplySeq on the object b.

所以,val (head :: tail)中的::指的是对象::(通过case定义类).

So, :: in val (head :: tail) refers to the object :: (defined through a case class).

这是一个类型声明,所以 ::[Int] 是一个类型.:: 本身是一个类,也是一个类型构造器(因为它构造给定类型参数的类型——::[Int] 是一种类型,::[String] 是另一种类型,等等).它也是List的子类,它只有两个子类:::Nil的单例类.

This is a type declaration, so ::[Int] is a type. :: itself is a class, and a type constructor as well (because it constructs types given a type parameter -- ::[Int] is one type, ::[String] is another type, etc). It is also a subclass of List, which has only two subclasses: :: and the singleton class of Nil.

这个声明是多余的,一般来说,人们几乎从不使用 :: 作为类型或类.我在这里展示它主要是为了完整性.

This declaration is superfluous, and, generally speaking, one hardly ever uses :: as a type or class. I show it here mostly for completeness.

这里,:: 是一个方法.它是List的一个方法,所以,既然Nil是一个List1不是,它一定属于到 Nil(或通过隐式转换可用).

Here, :: is a method. It is a method of List, so, since Nil is a List and 1 is not, it must belong to Nil (or be available through implicit conversion).

这里的注意机制是以 : 结尾的方法,当用于中缀运算符表示法时,绑定到右侧而不是左侧.或者,换句话说,a :: b 等价于 b.::(a).

The mechanism of note here is that methods ending with :, when used in infix operator notation, bind to the right instead of to the left. Or, in other words, a :: b is equivalent to b.::(a).

这种机制很少使用,我怀疑主要是为了让习惯了 fp 的程序员更熟悉传统的 fp 列表算法.它在 Scala 标准库上的其他一些地方也有使用.

This mechanism is rarely used and, I suspect, made mostly to make traditional fp list algorithms more familiar to programmers used to fp. It is used in a few other places on Scala standard library and out of it.

例如,在 Scala 2.8 上,现在有 +:,它与 :: 的用途相同,但为所有 Seq 定义>.它被 :+ 镜像,它附加元素,并且它的 : 除了消除它与 + 的歧义之外没有任何作用,它被重载以连接字符串.

On Scala 2.8, for instance, there's now +:, which serves the same purpose of ::, but is defined for all Seq. It is mirrored by :+, which appends elements, and whose : serves no purpose other than disambiguate it from +, which is overloaded to concatenate strings.

这篇关于Scala 和 :: 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆