groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "" <>
Subject dynamically attached object/added ivar
Date Fri, 12 Feb 2016 11:20:35 GMT
Hello there,

have we in Groovy an API which would serve as a reliable replacement of “dynamically attached
objects” or “dynamically added ivars” of other APIs? I mean a way to reasonably implement
something like "x_attach" and "x_attached" here:

def foo=... // any object of any class, typically some Map, but not necessarily
def tag=... // any object of any class, possibly even a class itself

def bar=foo // just to emphasize the attachment is object's, not variable's property

... throughout the code, "foo" and "bar" can be used normally, all APIs and libraries etc.
work with it the very same way they would without "x_attach" ...

assert bar.x_attached().is(tag) // but later, I can get the attached object

When the object foo/bar gets garbage-collected, it's attachment loses an active link and (presumed
it is not referenced from another place) gets garbage-collected too. Due to this demand, it
is rather inconvenient to simulate the attachments by a static map indexed e.g., by System.identityHashCode(foo),
for -- far as I know at least -- there is no convenient API to determine when a generic object
(whose finalize I cannot override) goes poof (and thus its attachment should be removed from
the map).

Thanks for any advice,

View raw message