Groovy - can a method defined in an interface have default values?

If the following is entered in Eclipse / STS (with groovy):

interface iFaceWithAnIssue {
    def thisIsFine(a,b,c)
    def thisHasProblems(alpha='va')
}

The only line that complains is the one that tries to use the default value. I can not tell from the codehaus website if this is supported or not.

IDE Error:

Groovy:Cannot specify default value for method parameter 

So this makes me think that this is not supported. Since there will be several implementations, I would like to use the interface here. I really don't need the default value in the interface, but there is an error trying to execute the interface contract if the implementation class then tries to set this argument by default. Is there any way?

+4
source share
1 answer

No, you can’t.

, Groovy , , :

class Test {
    void something( a=false ) {
        println a
    }
}

public void something(java.lang.Object a) {
    this.println(a)
}

public void something() {
    this.something(((false) as java.lang.Object))
}

, .

:

interface iFaceWithAnIssue {
    def thisHasProblems()
    def thisHasProblems(alpha)
}

class Test implements iFaceWithAnIssue {
    // This covers both Inteface methods
    def thisHasProblems(alpha='va') {
        // do something
    }
}
+8

All Articles