On Mon, May 3, 2021 at 3:35 AM Stephen R. van den Berg <[email protected]> wrote:
>
> Lance Dillon wrote:
> >The closest I could see is to not really have the functions directly, I
> >guess, but an mapping of functions, and overload `() so that it pulls the
> >function from the mapping, then you could easily replace the function by
> >replacing the reference in the mapping.
> >The replaced function would be locator collected,or you could implement it
> >as a list of hooks.
> >Could be transparent to calling code and be effectively the same thing, even
> >if implemented differently.
> >Unless I am misunderstanding how `() works.
>
> I think that method breaks at the point where you need to manipulate "this"
> to point to the main class.
>
> Ah well, I am restructuring the code so that the live class in question
> is being referenced through a variable. It seems that is the only
> practical solution.
My usual practice is to completely recompile the class, and have a
single mapping for all "carry-over" state, something like this:
class A {
mapping s = ([]);
void B() {...}
void C() {...}
}
And then to replace anything, I'd create a new instance of the
newly-compiled class, replace its empty state mapping with the same
one as the old object, and start using that. It's simple, reliable,
and safe (if any old code is still referenced, it'll use the same
state mapping), and doesn't require weird shenanigans.
It might be kinda nice to have some sort of compiler support for
changing the 'this' context of a function, but it's really not
something that I'd use very often.
ChrisA