Is there a way to create dot-free dsl in scala with two identifiers between variables?

scala

Solution

You can ask the parser:

$ 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.

However, there is a problem: This only works as long as `variable2` is an identifier (so that it can be used as a method name). With

scala> 1 equals to 2

already the parser fails:

<console>:1: error: ';' expected but integer literal found.
       1 equals to 2
                   ^

Parentheses are really your only way around(*):

scala> 1 equals to (2)
// ...
val res1 = 1.equals(to(2))

(*) unless you make `2` an identifier by using it with backticks

scala> 1 equals to `2`
// ...
val res2 = 1.equals(to).2

… nah, maybe not.

Problem

Is there a way to define a dsl, that would allow the following form? ``` variable identifier identifier variable ``` For example: ``` 1 equals to 2 ``` I know how to create a simpler form: `1 equals to (2)`, but I want to avoid parentheses. Is there a way to do it?

Original source

Related problems