ken
ken

Reputation: 3745

Resolving Groovy Map class

Anyone can explain why calling [:].class on a map return null while calling [:].getClass() return the expected result Map. See example below

1-

["test",[test:"test"],23].each {     
  println it.class 
}
class java.lang.String
null
class java.lang.Integer

2-

["test",[test:"test"],23].each {     
  println it.getClass()
}
class java.lang.String
class java.util.LinkedHashMap
class java.lang.Integer

Ken

Upvotes: 7

Views: 1136

Answers (1)

hvgotcodes
hvgotcodes

Reputation: 120318

Here is the answer

https://issues.apache.org/jira/browse/GROOVY-1824

EDIT -- sure. I think the semantics of a Map are such that if you have

def m = [one:1, two:2]

you are supposed to be able to access the entries in the map like

m.one

in other words, access into the map is like getting a property on the map object. If

m.class 

returned the class, it would break those semantics, because 'class' is not a key added to the map by the programmer.

Thats what I gather....

Upvotes: 11

Related Questions