About declaring the field and property with same name

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

About declaring the field and property with same name

Daniel Sun
Hi all,

         I found Groovy allows declaring the field and property with same name, e.g.

1)
https://github.com/apache/groovy/blob/master/src/test/gls/scope/MultipleDefinitionOfSameVariableTest.groovy#L110

2)
https://github.com/apache/groovy/blob/master/src/test/gls/scope/MultipleDefinitionOfSameVariableTest.groovy#L119

         As we all know, the field and property with same name will confuse developers, so I wonder why we should support the feature?

Cheers,
Daniel.Sun
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About declaring the field and property with same name

Daniel Sun
Hi all,

        If no one is opposed to deprecating the weird feature, I'll modify the two tests at the weekend(shouldCompile -> shouldNotCompile):

https://github.com/apache/groovy/blob/master/src/test/gls/scope/MultipleDefinitionOfSameVariableTest.groovy#L110

https://github.com/apache/groovy/blob/master/src/test/gls/scope/MultipleDefinitionOfSameVariableTest.groovy#L119


Cheers,
Daniel.Sun
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About declaring the field and property with same name

Jochen Theodorou


On 12.07.2017 17:48, Daniel Sun wrote:
> Hi all,
>
>         If no one is opposed to deprecating the weird feature, I'll modify
> the two tests at the weekend(shouldCompile -> shouldNotCompile):
>
> https://github.com/apache/groovy/blob/master/src/test/gls/scope/MultipleDefinitionOfSameVariableTest.groovy#L110
>
> https://github.com/apache/groovy/blob/master/src/test/gls/scope/MultipleDefinitionOfSameVariableTest.groovy#L119
>

sorry, but veto.

class X {
   String foo
   private foo
}

declares for example a get/setFoo for String, while the backing field is
Object. The field also allows you to use additional modifiers like
volatile for example.

bye Jochen
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About declaring the field and property with same name

Daniel Sun
Hi Jochen,

      OK, I see... but the feature is really wired, I don't know why we need it.

Cheers,
Daniel.Sun
--------------------------------------------
class Person {
    String name = '123'
    private name = 123
}

new Person().getName().class // class java.lang.Integer

--------------------------------------------
class Person {
    String name = '123'
    private name = 123
   
    public String getName() {
        return this.name;
    }
}

new Person().getName().class  // class java.lang.String
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About declaring the field and property with same name

Jochen Theodorou


On 13.07.2017 05:44, Daniel Sun wrote:
[...]

> --------------------------------------------
> class Person {
>     String name = '123'
>     private name = 123
> }
>
> new Person().getName().class // class java.lang.Integer
>
> --------------------------------------------
> class Person {
>     String name = '123'
>     private name = 123
>
>     public String getName() {
>         return this.name;
>     }
> }
>
> new Person().getName().class  // class java.lang.String

I think we should set the rule that if you redefine a field for a
property, you should not be able to define an initializer for both


bye Jochen
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: About declaring the field and property with same name

Daniel Sun
Hi Jochen,

      Could you provide me some example on the feature? To be honest, I can not find any use case... Thanks in advance.

Cheers,
Daniel.Sun
Loading...