Scala右关联方法 [英] Scala right associative methods

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

问题描述

我正在学习Scala,并且正在与正确的未使用对象相关联玩耍.我知道,如果名称以:"结尾,那么它将变为正确的关联.但是,似乎对命名有一些奇怪的限制

I am learning Scala, and playing with right associate unapply object. I know that if the name ends with ':' then it becomes right associative. However, there seems to be some strange restrictions on the naming

例如 这些无效

object cons: { def unapply(value: String): Option[(Char, List[Char])] = ??? }
object :_cons_: { def unapply(value: String): Option[(Char, List[Char])] = ??? }

这些有效

object cons_: { def unapply(value: String): Option[(Char, List[Char])] = ??? }
object >>: { def unapply(value: String): Option[(Char, List[Char])] = ??? }

因此,在标识符中混合使用字母数字字符和符号似乎有些怪异.

So there seems to be some weirdness about mixing alpha-numeric characters and symbols in identifiers.

因此,基本上,我想使用一个描述性名称,即"cons",并且仍然具有正确的关联性.另外,出于美学原因,我希望我的运算符是对称的:-),所以我真的不喜欢cons_:
有没有办法在不使用冒号的情况下将某些东西关联到右侧?或任何其他建议来实现这一目标?

So basically, I want to have a descriptive name i.e. 'cons' and still have right associativity. Also, I would like my operator to be symetric for aesthetic reasons :-), so I dont really like cons_:
Is there a way to make something associate to the right without using a colon? Or any other suggestions to achieve this?

:_cons_:似乎是最接近的,但是由于某些原因,标识符不能以':'开头并且具有字母数字

:_cons_: seems to be the closest, but, for some reason the identifier can't start with ':' and have alphanumerics

推荐答案

来自

有三种形成标识符的方式.首先,标识符可以 以一个字母开头,后跟任意顺序的 字母和数字.后面可能跟着下划线"_" 另一个字符串由字母和数字或 运算符.其次,标识符可以以运算符开头 字符,后跟任意的运算符字符序列. 前两种形式称为 plain 标识符.最后, 标识符也可以由之间的任意字符串形成 反引号(主机系统可能会对哪些限制 字符串对于标识符是合法的).然后,标识符由 除反引号本身以外的所有字符.

There are three ways to form an identifier. First, an identifier can start with a letter which can be followed by an arbitrary sequence of letters and digits. This may be followed by underscore ‘_’ characters and another string composed of either letters and digits or of operator characters. Second, an identifier can start with an operator character followed by an arbitrary sequence of operator characters. The preceding two forms are called plain identifiers. Finally, an identifier may also be formed by an arbitrary string between back-quotes (host systems may impose some restrictions on which strings are legal for identifiers). The identifier then is composed of all characters excluding the backquotes themselves.

因此,您似乎很不走运-如果您的标识符以:开头,则它不能包含非操作符.但是请注意,您可以编写以下代码(这无意于做任何有意义的事-只是为了演示语法):

So it looks like you're out of luck—if your identifier starts with a : it can't contain non-operator characters. Note, though, that you can write the following (which isn't intended to do anything meaningful—just to demonstrate the syntax):

scala> class X { def `:cons:`(i: Int) = i }
defined class X

scala> val x = new X
x: X = X@6a665da6

scala> 1 `:cons:` x
res1: Int = 1

方法名称仍以​​冒号结尾,因此您可以找到所需的正确关联性.

The method name still ends with a colon, so you get the right associativity you're looking for.

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

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