是否有一个类似于Haskell的$(美元符号)的Scala运算符? [英] Is there a Scala operator similar to Haskell's $ (dollar sign)?

查看:106
本文介绍了是否有一个类似于Haskell的$(美元符号)的Scala运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像在Haskell中使用$强制在Scala中强制运算符优先级?

Is there any way to force operator precedence in Scala like you do in Haskell with $?

例如,在Haskell中,您拥有:

For example, in Haskell, you have:

a b c = ((a b) c)

a $ b c = a (b c)

在Scala中有类似的方法吗?我知道Scala本身没有运算符,但是有没有办法实现类似的效果?

Is there a similar way to do this in Scala? I know Scala doesn't have operators per se, but is there a way to achieve a similar effect?

推荐答案

中缀vs.符号通常以类似的方式用于控制优先级:

infix vs . notation is often used in a similar fashion to control precedence:

a b c d == a.b(c).d
a.b c d == a.b.c(d)
a b c.d == a.b(c.d)

Scala对于固定符号中使用的运算符也有固定的优先顺序:

Scala also has a fixed precedence ordering for operators used in infix notation:

(all letters)
|
^
&
< >
= !
:
+ -
* / %
(all other special characters)

通常可以显式选择名称以利用此优势.例如,标准解析器库中的~^^.

Names can usually chosen explicitly to take advantage of this. For example, ~ and ^^ in the standard parsers library.

这篇关于是否有一个类似于Haskell的$(美元符号)的Scala运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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