in reply to Action at a distance

I'm not an expert on Math::BigInt, but it seems to create objects and by copying you are always accessing the same instance.

And when searching for a clone method I found this in the documentation

$y = $x->copy();         # make a copy (unlike $y = $x)

So maybe try this?

HTH! :)

update

> This behaviour can be avoided if I use the overloaded ++ operator instead of the binc() method call:

Well you could "overload" assignments via tie, to always do a copy ...

see overload#Overloadable Operations:

> Simple assignment is not overloadable (the = key is used for the "Copy Constructor"). Perl does have a way to make assignments to an object do whatever you want, but this involves using tie(), not overload - see "tie" in perlfunc and the "COOKBOOK" examples below.

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: Action at a distance
by syphilis (Archbishop) on Nov 03, 2022 at 12:42 UTC
    well you could "overload" assignments via tie to always do a copy

    Yes - that might be what I'm after. I'm looking for a solution that would be applied inside Math::BigInt.
    I'll see if I can get my head around it. (Thanks for the link.)

    I actually struck the problem in Math::MPFR (which is where I'd like to fix it), but used Math::BigInt as the demo because everyone already has that module and I believe that it's the same issue with both of those modules.

    Cheers,
    Rob
Re^2: Action at a distance (updated)
by ikegami (Patriarch) on Nov 03, 2022 at 14:29 UTC

    I wouldn't go with tie (if it even works?). I'd overload = to automatically clone the object on assignment.

    Another approach is to use immutable objects. Such a class wouldn't provide a method for incrementing the object; it would provide a method that returns a new object with the higher value.

    Both of these approaches add the unnecessary computational and memory overhead of creating clones in situations where they aren't needed.

      > I'd overload = to automatically clone the object on assignment.

      Sorry, that's too easy to misunderstand.

      Let me be more precise:

      $b = $a with = overloaded to ->clone will not do an immediate $b =  $a->clone °

      It's rather a kind of copy-on-write.

      After the assignment the refs will still be identical : $b == $a

      The ->clone ("copy" in COW) will only happen delayed just prior to changing $b or $a ("write" in COW)

      But the wording is fuzzy and needs to be tested.

      Anyway this could indeed fix the problem of the OP in an efficient way.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

      °) In Math::BigInt the cloning is done with the method ->copy

Re^2: Action at a distance (updated)
by ikegami (Patriarch) on Nov 03, 2022 at 14:15 UTC

    You can overload = to get the desired effect. But that only works if you have immutable objects.

      > You can overload = to get the desired effect.

      Not sure what you mean, the reason why I've put "overload" in double-quotes is that it's not possible to change° assignment via overload.

      One needs to tie the obj-ref too, see the linked doc.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

      update

      °) well, not the immediate assignment, see Re^3: Action at a distance

        Sorry, I hadn't read the post script. I was replying to the tie bit.