[ 
https://issues.apache.org/jira/browse/GROOVY-5491?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

John Wagenleitner updated GROOVY-5491:
--------------------------------------
    Description: 
Creating a subclass of java.util.HashMap with properties defined results in 
inconsistent and confusing behavior. 
{code:java}
class MapSub extends HashMap {
  def foo
  
}

// Using dot notation to get the property results in a map key lookup.
def m = new MapSub(foo: 'bar')
println m.foo    // prints 'null'
println m.getFoo() // prints 'bar'

// Using same dot notation to set the property results in property setter.
m.foo = 'baz'
println m.foo // still prints 'null'
println m.getFoo() // prints 'baz'
{code}
This behavior results in subtle and hard to track bugs. I saw another Jira that 
stated that a design decision was made that Map dot property getters always 
resulted in a map key lookup. For consistency, I think either the same policy 
should be applied to Map property setters or the original design decision 
should be reconsidered to first check for property existence.


  was:
Creating a subclass of java.util.HashMap with properties defined results in 
inconsistent and confusing behavior. 
{code:groovy}
class MapSub extends HashMap {
  def foo
  
}

// Using dot notation to get the property results in a map key lookup.
def m = new MapSub(foo: 'bar')
println m.foo    // prints 'null'
println m.getFoo() // prints 'bar'

// Using same dot notation to set the property results in property setter.
m.foo = 'baz'
println m.foo // still prints 'null'
println m.getFoo() // prints 'baz'
{code}
This behavior results in subtle and hard to track bugs. I saw another Jira that 
stated that a design decision was made that Map dot property getters always 
resulted in a map key lookup. For consistency, I think either the same policy 
should be applied to Map property setters or the original design decision 
should be reconsidered to first check for property existence.



> HashMap subclass property access inconsistent
> ---------------------------------------------
>
>                 Key: GROOVY-5491
>                 URL: https://issues.apache.org/jira/browse/GROOVY-5491
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 1.8.6
>            Reporter: Michael Collado
>            Priority: Minor
>
> Creating a subclass of java.util.HashMap with properties defined results in 
> inconsistent and confusing behavior. 
> {code:java}
> class MapSub extends HashMap {
>   def foo
>   
> }
> // Using dot notation to get the property results in a map key lookup.
> def m = new MapSub(foo: 'bar')
> println m.foo    // prints 'null'
> println m.getFoo() // prints 'bar'
> // Using same dot notation to set the property results in property setter.
> m.foo = 'baz'
> println m.foo // still prints 'null'
> println m.getFoo() // prints 'baz'
> {code}
> This behavior results in subtle and hard to track bugs. I saw another Jira 
> that stated that a design decision was made that Map dot property getters 
> always resulted in a map key lookup. For consistency, I think either the same 
> policy should be applied to Map property setters or the original design 
> decision should be reconsidered to first check for property existence.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to