groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paolo Di Tommaso <paolo.ditomm...@gmail.com>
Subject Re: Custom operator for Groovy 3
Date Sun, 30 Oct 2016 09:39:44 GMT
Hi Daniel,

I haven't had realised the need of the backtick characters to define and
use custom operators. Still useful though much less *sexy* IMO.



Cheers,
Paolo

On Sun, Oct 30, 2016 at 10:30 AM, Daniel.Sun <realbluesun@hotmail.com>
wrote:

> Hi Paolo,
>
>     Here are some example code. As you see, the special character(`) is
> required to define the custom operator:
>
> // Custom operators should be declared first, then use.
> // It can be declared in the class or script, but it must be declared
> static.
> class A {
>     static "`>?`"(int a, int b) {
>         return Math.max(a, b);
>     }
> }
>
> static "`<?`"(int a, int b) {
>     return Math.min(a, b);
> }
>
> // Notice: the custom operator has higher priority to avoid parentheses in
> most cases
> assert 2 == 1 `>?` 2
> assert 1 == 1 `<?` 2
> assert 7 == (1 + 3) `>?` (2 + 5)
> assert 9 == 1 + 3 `>?` 2 + 5
>
>
>
> --
> View this message in context: http://groovy.329449.n5.
> nabble.com/Custom-operator-for-Groovy-3-tp5736388p5736394.html
> Sent from the Groovy Dev mailing list archive at Nabble.com.
>

Mime
View raw message