有没有办法在scala中创建无点dsl,在变量之间有两个标识符?

Rog*_*ach 8 scala

有没有办法定义一个dsl,这将允许以下形式?

variable identifier identifier variable
Run Code Online (Sandbox Code Playgroud)

例如:

1 equals to 2
Run Code Online (Sandbox Code Playgroud)

我知道如何创建一个更简单的形式:1 equals to (2)但我想避免使用括号.有办法吗?

Deb*_*ski 13

你可以问解析器:

$ scala -Xprint:parser
Welcome to Scala version 2.9.2 ... blah

scala> variable1 identifier1 identifier2 variable2
// lots of stuff and inside:
val res0 = variable1.identifier1(identifier2).variable2
// this is how the parser sees it.
// if you can make that work (dynamic classes…?), you’re good to go.
Run Code Online (Sandbox Code Playgroud)

但是,存在一个问题:只有variable2标识符才能使用(因此它可以用作方法名称).同

scala> 1 equals to 2
Run Code Online (Sandbox Code Playgroud)

已经解析器失败:

<console>:1: error: ';' expected but integer literal found.
       1 equals to 2
                   ^
Run Code Online (Sandbox Code Playgroud)

圆括号是你唯一的方法(*):

scala> 1 equals to (2)
// ...
val res1 = 1.equals(to(2))
Run Code Online (Sandbox Code Playgroud)

(*)除非您2使用反引号制作标识符

scala> 1 equals to `2`
// ...
val res2 = 1.equals(to).2
Run Code Online (Sandbox Code Playgroud)

......不,也许不是.