Hi Jochen, I tried to implement custom operator based on your new idea and found it a bit ambiguous, e.g. ".<" is ambiguous to "obj.hello('Daniel')", ".*" is ambiguous to "import java.util.*", etc. So I reserve the "`" notation for the time being.
To extend your new idea: a `>?` b === a."`>?`"(b) // binary expression the following syntax will be useful too: `?` a === "`?`"(a) // unary expression a `?` === a."`?`"() // postfix expression As you can see, with double "`" quoted, all the three syntax are natural. Any thoughts? ;-) Cheers, Daniel.Sun 在 "Jochen Theodorou [via Groovy]" <ml-node+s329449n5736402...@n5.nabble.com>,2016年10月30日 下午8:16写道: 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/is-it-possible-to-define-a-new-operator-in-groovy/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 ________________________________ If you reply to this email, your message will be added to the discussion below: http://groovy.329449.n5.nabble.com/Custom-operator-for-Groovy-3-tp5736388p5736402.html To unsubscribe from Custom operator for Groovy 3, click here<http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5736388&code=cmVhbGJsdWVzdW5AaG90bWFpbC5jb218NTczNjM4OHwxMTQ2MjE4MjI1>. NAML<http://groovy.329449.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> -- View this message in context: http://groovy.329449.n5.nabble.com/Custom-operator-for-Groovy-3-tp5736388p5736431.html Sent from the Groovy Dev mailing list archive at Nabble.com.