Hi,

I have a module A that overloads the '-' operator via its A::oload_minus() subroutine.
And I have a second module B that also overloads the '-' operator via it's own B::oload_minus() subroutine.
Both modules also have their own oload_add, oload_mul, oload_div, oload_mod and oload_pow subroutines that overload the other basic arithmetic operators).

I have constructed the overloading in module A to handle B objects.
But if module B's overloading subroutines are passed a module A object, it is (by my current design) a fatal error.
use A; use B; $A_obj = A->new(16); $B_obj = B->new(6); my $n1 = $A_obj - $B_obj; # $n1 is an A object with value 10 my $n2 = $B_obj - $A_obj; # Fatal error
In the above demo I want $n2 to be an A object, with the value of -10.
That is, I want the A::oload_minus() sub to receive the args ($A_obj, $B_obj, TRUE).
Instead, the B::oload_minus() sub is receiving the args ($B_obj, $A_obj, FALSE) - which is, by my current design, a fatal error since B::overload_minus() does not currently accept A objects.

Is there a way that I can work around this without making any changes to the B module ? (The motivation to not alter module B is simply that I don't want to add more clutter to B unless I need to.)

My module "A" is in fact Math::MPC, and my module "B" is in fact Math::MPFR.

AFTERTHOUGHT: I should point out that the arithmetic overloading in the publicly available versions of Math::MPC don't yet accept Math::MPFR objects. (I've currently implemented this new feature on my local Math::MPC builds only.)

Cheers,
Rob

In reply to A little overloading conundrum by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.