On Mon, May 3, 2021 at 2:04 AM Stephen R. van den Berg <[email protected]> wrote:
>
> Say I have this:
>
> class A {
>   int k;
>   void B() {
>     write("foo %d\n", k);
>   }
>   void C() {
>     k = 3;
>     write("bar\n");
>   }
> }
>
> int main() {
>   A a = A();
>   a->C();       // Displays: bar
>   a->B();       // Displays: foo 3
>   // At this point I want to replace the function B
>   // in the running/compiled instance of A in a.
>   //  I.e. I want to call compile() or similar
>   //  on the following code:
>   //    void B() { write("FOO %d\n", k); }
>   //  such that I can subsequently run:
>   a->B();      // Should display: FOO 3
>   return 0;
> }
>
> Any way this can be accomplished?
> An alternate way would be to compile the whole class of A again, but
> then run method B() in it with a custom this argument pointing to the
> old instance in a.  Is that possible?  I know javascript can do this,
> but it seems like Pike does not allow/support it.

JavaScript has some utterly bizarre rules about the 'this' reference
that I wouldn't want to see any other language replicate. In Pike, a
function remembers the context it was created in, so what you're
trying to do won't work with injection. But perhaps subclassing can do
what you want - instead of compiling the entire class again, create a
subclass that replaces that one function.

ChrisA

Reply via email to