You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Saravanan Palanichamy <ch...@gmail.com> on 2021/01/05 03:22:21 UTC

Default values for parameters in interface methods

Hello

Are there plans to support default parameters in interface methods

interface MyClass {
    def myFunction(String parameter1 = "1234")
}

the error I get is Cannot specify default value for method parameter inside an interface

regards
Saravanan

Re: Default values for parameters in interface methods

Posted by Paul King <pa...@asert.com.au>.
Yes, default methods would be the right approach here. In Groovy 3, we
currently use traits under the covers to implement default methods in
interfaces. The plan was to output native default methods in Groovy 4
(maybe always, maybe just with @CompileStatic) and then the default
parameter values might come almost for free.

On Wed, Jan 6, 2021 at 8:23 AM MG <mg...@arscreat.com> wrote:

> Yes, sorry, indeed there is Jochen: I overlooked that the reply was by
> you and not the initial poster ;-)
>
> Since we use Groovy only, I have no experience how Groovy traits behave
> for Java cross-compilation, but supporting default parameters in
> interfaces sounds like a good idea in principle regardless, since it is
> one of those "why would that _not_ be supported here ?" scenarios...
>
> Cheers,
> mg
>
>
> On 05/01/2021 17:30, Jochen Theodorou wrote:
> >
> > I think there is a misunderstanding...
>
>
>

Re: Default values for parameters in interface methods

Posted by MG <mg...@arscreat.com>.
Yes, sorry, indeed there is Jochen: I overlooked that the reply was by 
you and not the initial poster ;-)

Since we use Groovy only, I have no experience how Groovy traits behave 
for Java cross-compilation, but supporting default parameters in 
interfaces sounds like a good idea in principle regardless, since it is 
one of those "why would that _not_ be supported here ?" scenarios...

Cheers,
mg


On 05/01/2021 17:30, Jochen Theodorou wrote:
>
> I think there is a misunderstanding...



Re: Default values for parameters in interface methods

Posted by Jochen Theodorou <bl...@gmx.org>.
I think there is a misunderstanding... in Java the interface from OP
would be

>      interface MyClass {
>           public default Object myFunction(){
>               return myFunction("1234)
>           }
> ;
>           public Object myFunction(String parameter1);
>      }


the compile could generate that

On 05.01.21 17:23, MG wrote:
> Maybe merging interface with trait could be possible in the future, but
> would using the "trait" keyword instead of "interface" have any negative
> effects in your scenario ?
> I only use trait where I need the added functionality, but technically
> it behaves just like a suped up interface afaiaao...
>
> Cheers,
> mg
>
> On 05/01/2021 17:14, Jochen Theodorou wrote:
>> Hi Paul,
>>
>> wouldn't it be possible to implement the default parameter now with
>> default methods?
>>
>> On 05.01.21 05:39, Paul King wrote:
>>> You could potentially use a trait?
>>>
>>> trait MyTrait {
>>>      abstract myFunction(String parameter1 = "1234")
>>> }
>>>
>>> class MyClass implements MyTrait {
>>>      def myFunction(String parameter1) {
>>>          parameter1.reverse()
>>>      }
>>> }
>>>
>>> assert new MyClass().myFunction() == '4321'
>>>
>>>
>>> On Tue, Jan 5, 2021 at 1:22 PM Saravanan Palanichamy <chavan77@gmail.com
>>> <ma...@gmail.com>> wrote:
>>>
>>>     Hello
>>>
>>>     Are there plans to support default parameters in interface methods
>>>
>>>     interface MyClass {
>>>          def myFunction(String parameter1 = "1234")
>>>     }
>>>
>>>     the error I get is Cannot specify default value for method parameter
>>>     inside an interface
>>>
>>>     regards
>>>     Saravanan
>>>
>>
>


Re: Default values for parameters in interface methods

Posted by MG <mg...@arscreat.com>.
Maybe merging interface with trait could be possible in the future, but 
would using the "trait" keyword instead of "interface" have any negative 
effects in your scenario ?
I only use trait where I need the added functionality, but technically 
it behaves just like a suped up interface afaiaao...

Cheers,
mg

On 05/01/2021 17:14, Jochen Theodorou wrote:
> Hi Paul,
>
> wouldn't it be possible to implement the default parameter now with
> default methods?
>
> On 05.01.21 05:39, Paul King wrote:
>> You could potentially use a trait?
>>
>> trait MyTrait {
>>      abstract myFunction(String parameter1 = "1234")
>> }
>>
>> class MyClass implements MyTrait {
>>      def myFunction(String parameter1) {
>>          parameter1.reverse()
>>      }
>> }
>>
>> assert new MyClass().myFunction() == '4321'
>>
>>
>> On Tue, Jan 5, 2021 at 1:22 PM Saravanan Palanichamy <chavan77@gmail.com
>> <ma...@gmail.com>> wrote:
>>
>>     Hello
>>
>>     Are there plans to support default parameters in interface methods
>>
>>     interface MyClass {
>>          def myFunction(String parameter1 = "1234")
>>     }
>>
>>     the error I get is Cannot specify default value for method parameter
>>     inside an interface
>>
>>     regards
>>     Saravanan
>>
>


Re: Default values for parameters in interface methods

Posted by Jochen Theodorou <bl...@gmx.org>.
Hi Paul,

wouldn't it be possible to implement the default parameter now with
default methods?

On 05.01.21 05:39, Paul King wrote:
> You could potentially use a trait?
>
> trait MyTrait {
>      abstract myFunction(String parameter1 = "1234")
> }
>
> class MyClass implements MyTrait {
>      def myFunction(String parameter1) {
>          parameter1.reverse()
>      }
> }
>
> assert new MyClass().myFunction() == '4321'
>
>
> On Tue, Jan 5, 2021 at 1:22 PM Saravanan Palanichamy <chavan77@gmail.com
> <ma...@gmail.com>> wrote:
>
>     Hello
>
>     Are there plans to support default parameters in interface methods
>
>     interface MyClass {
>          def myFunction(String parameter1 = "1234")
>     }
>
>     the error I get is Cannot specify default value for method parameter
>     inside an interface
>
>     regards
>     Saravanan
>


Re: Default values for parameters in interface methods

Posted by Paul King <pa...@asert.com.au>.
You could potentially use a trait?

trait MyTrait {
    abstract myFunction(String parameter1 = "1234")
}

class MyClass implements MyTrait {
    def myFunction(String parameter1) {
        parameter1.reverse()
    }
}

assert new MyClass().myFunction() == '4321'


On Tue, Jan 5, 2021 at 1:22 PM Saravanan Palanichamy <ch...@gmail.com>
wrote:

> Hello
>
> Are there plans to support default parameters in interface methods
>
> interface MyClass {
>     def myFunction(String parameter1 = "1234")
> }
>
> the error I get is Cannot specify default value for method parameter
> inside an interface
>
> regards
> Saravanan
>