groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daniel.Sun" <realblue...@hotmail.com>
Subject Re: Custom operator for Groovy 3
Date Sun, 30 Oct 2016 09:30:37 GMT
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