Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^3: How does Math::GMP overload the assignment operator ?

by LanX (Sage)
on Nov 08, 2020 at 10:43 UTC ( #11123500=note: print w/replies, xml ) Need Help??

in reply to Re^2: How does Math::GMP overload the assignment operator ?
in thread How does Math::GMP overload the assignment operator ?

> I find this overloading of = to be a bit of a headache,

Me too. For the same reasons

  1. it's a misnomer because not overloading assignment but only a special Copy-On-Write behavior. (you need tied variables to control assignment)
  2. it assumes that copying on assignment is the DWIM behavior, but only when one of the values is changing. I for my part would be very surprised. an explicit ->clone method (or operator?) might have been the better choice.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^4: How does Math::GMP overload the assignment operator ?
by syphilis (Bishop) on Nov 09, 2020 at 02:58 UTC
    There's no denying that being able to do $clone = $orig is very convenient if you both understand the limitations, and make sure you work around them.
    It's quicker to type, and also easier to remember, than $clone = $orig->gmp_copy()
    But, as an author who has enabled this convenience in his modules, is it acceptable that I don't document the traps that users might fall into when they avail themselves of this option ?

    With Math::GMP (which I've neither authored or maintained), I initially considered it smart of them to not explicitly overload '=', and to also not mention that this option exists.
    I thought that, in doing this, they were taking advantage of the fact that there's no onus upon them to document anything at all about this option.
    And I also thought that I might take the same path with my modules.
    However, I'm no longer so sure about the validity of that thinking - and I think, in my modules, I will make some brief mention of the traps, along with a recommendation to read the overload documentation carefully.

    It occurs to me that, if one wanted to code defensively, doing $clone = $orig + 0 is safer than doing $clone = $orig.


      The real solution to this is to make your objects immutable. Don't provide any methods that change the value of your object, and instead return new objects. You could then remove your += and similar overloads and = overload, and just rely on creating new objects from your + overload.

      It would actually be possible to still provide the += overload as an optimization if you were providing the = overload. Since the only way to trigger mutation of the object would be via the overload, perl would protect you from ever modifying a shared object by cloning using the = overload. I'm not sure that the extra complexity here would really be worth it.

        The real solution to this is to make your objects immutable

        That (along with the elaboration you've provided) seems to be describing exactly what Math::GMP does.
        And yet Math::GMP also sets that very same troublesome trap wrt overloading '=' that I find disconcerting.
        So, I'm left wondering just what it is that this "solution" solves.

        I'll point out that I don't mean that last sentence to sound as smartarsey as it does. Deep down I know the problem is that I'm being dense.

        «The real solution»

        Yet another ToE or another Promised Land? It would be nice if you could provide a simple example for the uninitiated. No kidding. Best regards.

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re^4: How does Math::GMP overload the assignment operator ?
by ikegami (Pope) on Nov 11, 2020 at 04:11 UTC

    Operator overloading make objects behave like primitive types, and you wouldn't use 3->clone() or "abc"->clone()

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11123500]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (None)
    As of 2021-10-16 16:16 GMT
    Find Nodes?
      Voting Booth?
      My first memorable Perl project was:

      Results (69 votes). Check out past polls.