Copy-on-demand type behaviour

I've been noodling on the notion of a "copy on demand" behaviour (as kind of a souped-up copy-on-write), for types derived from other types (e.g. a slice of a collection). i.e. where an object references another object until that other object goes away naturally, immediately prior to which it copies what it needs from it. The idea being to avoid unnecessary copies entirely (in the case that the source object outlives the dependant).

Implementing this doesn't seem entirely trivial, so before I get really into it I want to check if anybody has pursued this idea already?

I'm pretty sure this comes down to "registering arbitrary callbacks on deinit [or mutate]". Which you can certainly do! Just be careful of object cycles.

1 Like