Default values for parameters in interface methods

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Default values for parameters in interface methods

Saravanan Palanichamy
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
Reply | Threaded
Open this post in threaded view
|

Re: Default values for parameters in interface methods

paulk_asert
Administrator
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 <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

Re: Default values for parameters in interface methods

Jochen Theodorou
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 <[hidden email]
> <mailto:[hidden email]>> 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
>

MG
Reply | Threaded
Open this post in threaded view
|

Re: Default values for parameters in interface methods

MG
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 <[hidden email]
>> <mailto:[hidden email]>> 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
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Default values for parameters in interface methods

Jochen Theodorou

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 <[hidden email]
>>> <mailto:[hidden email]>> 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
>>>
>>
>

MG
Reply | Threaded
Open this post in threaded view
|

Re: Default values for parameters in interface methods

MG
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...


Reply | Threaded
Open this post in threaded view
|

Re: Default values for parameters in interface methods

paulk_asert
Administrator
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 <[hidden email]> 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...