Details
-
Sub-task
-
Status: Open
-
Major
-
Resolution: Unresolved
-
1.7.6
-
None
-
None
-
Linux, Fedora 13, Vaio F12
Description
As per this thread on Groovy User:
http://groovy.329449.n5.nabble.com/Cannot-create-static-property-via-metaClass-td3792536.html#a3832577
I am unable to do the following:
class Foo {}
Foo.metaClass.static.bar = 1
Foo.bar // error, no such property bar for class Foo
Suggested non-working workaround:
class Foo {}
Foo.metaClass.static.getBar =
Foo.metaClass.static.setBar =
println Foo.bar // 1
Foo.bar = 5
println Foo.bar // obviously, still 1 since static bar property is never created, just creating static getter/setter for a non-existent foo
Interestingly, running above code once, and then swapping getBar=
{1}with getBar=
{bar}, the code works and one can overwrite the initial value.
On a side note: when we are able to create an MCL static properties, what will the scope be? i.e. same as
class Foo {
static bar
}
I ask because I am unsure where groovy MOP and JVM garbage collection come into play. I stumbled across this problem as I was attempting to create a per request singleton (via MCL), as opposed to application/instance lifetime scoped with
class Foo {
private static final bar = new Thing()
}
Thanks for evolving Groovy, now & the future are very bright.