On 30.10.2016 11:07, Daniel.Sun wrote:
> Hi Paolo,
>
> There are some limitations when we use antlr to define grammar, so the
> backticks are used to distinguish with other operators.
>
> I saw an answer from Jochen(shown as follow), so I tryied to add this
> experimental feature
>
> "We always wanted the ability to define an operator through the user in
> Groovy, but so far we haven't gotten around the problems that come along
> with that."
> http://stackoverflow.com/questions/6485861/isitpossibletodefineanewoperatoringroovy/10848093#10848093
I actually gave up on custom operators for a bit.
In Scala you have a natural mapping of "a + b" to a."+"(b). But in
Groovy that would be a(+).b. That means binary expressions and command
expressions follow different patterns. That means in a+b+c we do not
naturally get (a+b)+c, we get a(+).b(+).c. This is what makes you
require special characters to distinguish operators and command
expressions in the usage of those
And if you change precedence rules like you described... a + b + c is
(a+b)+c while a + b "`x`" c would become a+(b "`x`" c) instead.
But to develop the idea... how about saying that everything between dot
and an identifier is an operator? So a .x c would be a.x(c).. well that
looks obvious, but a .>? c becomes a.">?"(c). This kind of logic would
reduce the need for special characters and be in line with existing
syntax. And at the same time precedence rule would be clear. a + b .>? c
would be a+(b.">?"(c)) and a .>? b +c would become (a.">?"(b))+c
bye Jochen
