groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From OC <>
Subject Re: dynamically attached object/added ivar
Date Fri, 12 Feb 2016 17:56:52 GMT

> Where have you seen such behavior before?

E.g., in Objective C; see its standard objc_setAssociatedObject API -- a pretty handy thing,
this one.

> Expando may be something to look at.

Far as I understand the thing, hardly; but I am far from an expert and can be missing worlds.
Could you perhaps outline how those two methods x_attach and x_attached would look like?

Thanks a big lot,

> On 12-Feb-2016 4:50 pm, "" <> wrote:
> 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
> foo.x_attach(tag)
> 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,
> OC

View raw message