[prev in list] [next in list] [prev in thread] [next in thread]
List: groovy-user
Subject: Re: [groovy-user] Map key confusion
From: "Steven Devijver" <steven.devijver () gmail ! com>
Date: 2007-01-29 18:48:47
Message-ID: 44b81f710701291048w4475f520xf50b8f2f7ec09b6f () mail ! gmail ! com
[Download RAW message or body]
Ok, I got this output from the groovy 1.0 console:
groovy> a = "key"
Result: "key"
groovy> map = [:]
Result: [:]
groovy> map[a] = "value"
Result: "value"
groovy> map[b] = "value"
Exception thrown: groovy.lang.MissingPropertyException: No such
property: b for class: Script3
groovy.lang.MissingPropertyException: No such property: b for class: Script3
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:888)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:2077)
at groovy.lang.GroovyObjectSupport.getProperty(GroovyObjectSupport.java:65)
at groovy.lang.Script.getProperty(Script.java:85)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getGroovyObjectProperty(ScriptBytecodeAdapter.java:560)
at Script3.run(Script3:1)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:484)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:425)
at gjdk.groovy.lang.GroovyShell_GroovyReflector.invoke(Unknown Source)
at groovy.lang.MetaMethod.invoke(MetaMethod.java:115)
at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:131)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy:503)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:97)
at groovy.ui.Console$_runScript_closure10.doCall(Console.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at groovy.lang.Closure.call(Closure.java:188)
at groovy.lang.Closure.call(Closure.java:183)
at groovy.lang.Closure.run(Closure.java:264)
at java.lang.Thread.run(Thread.java:595)
groovy> map.a = "value2"
Result: "value2"
groovy> println(map)
["key":"value", "a":"value2"]
groovy> map.b = "value2"
Result: "value2"
groovy> map = [ a : "value1", (a) : "value2", b : "value3" ]
Result: ["key":"value2", "b":"value3", "a":"value1"]
groovy> println(map)
["key":"value2", "b":"value3", "a":"value1"]
I guess what was confusing me is that keys are evaluated when creating
entries one an existing map with square brackets. I don't see why we
need this for we have parentheses if we want evaluation.
At least in grails evaluation is much less common than strings as
keys. I now have a much better understanding of how map keys work in
groovy.
Thanks for your feedback.
Steven
On 1/29/07, Russel Winder <russel@russel.org.uk> wrote:
> On Mon, 2007-01-29 at 09:55 +0100, Steven Devijver wrote:
>
> > Thanks for clarifying. I wouldn't go as far as saying it makes sense
> > but at least I know how to avoid the error.
>
> Steven,
>
> What would make sense for you? If you can show a couple of examples of
> what makes sense for you, I can have a go at trying to explain the
> difference between that and Groovy as is which may help understanding
> for you.
>
> Speaking from a position of deep ignorance about this implementation
> issues :-) I think the automated stringification of the key in a map
> literal is required for some of the higher builder magic.
> --
> Russel.
> ====================================================
> Dr Russel Winder +44 20 7585 2200
> 41 Buckmaster Road +44 7770 465 077
> London SW11 1EN, UK russel@russel.org.uk
>
>
>
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic