Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Overloading multiplication involving BigInt

by pileofrogs (Priest)
on Jan 13, 2012 at 21:43 UTC ( #947837=note: print w/replies, xml ) Need Help??

in reply to Overloading multiplication involving BigInt

No one else is commenting, so I'll take a wild stab.

Why not add more elsif branches to your sub? You say you're worried about bmul behaving badly if it's handed something other than a BigInt, so why not just check for it?

You could also run some tests or read the source to find out what bmul really does if the 2nd operand isn't a BigInt.

Why would you have to explicitly bless your integers if you made a subclass of Math::BigInt?

And to step back a little: are you sure you want to do overloading? Will future users (or yourself) be flummoxed that $a*$b isn't doing what they expect? Is typing '$obj->mult($other_thing)' really that bad? At least that way you know exactly what you just did.

Okay, that's about all I can think of. I hope I was helpful.


  • Comment on Re: Overloading multiplication involving BigInt

Replies are listed 'Best First'.
Re^2: Overloading multiplication involving BigInt
by grondilu (Friar) on Jan 14, 2012 at 15:17 UTC

    Well, I had a look at Math::BigInt source code and it seemed to me that the '*' overloading had already a lot of elsif branches. I just don't want to reinvent the wheel and redo what has already been done. Basically I whish I could just say "if the second operand is a My::Custom::Object then do this, otherwise just do whatever '*' does in the Math::BigInt class".

    About explicit blessing, I just don't see how I could do it otherwise, unless I use a constructor anyway. But now that you make me think of it , I wonder if the magic of bigint wouldn't be inherited. I have to check this out, indeed.

    You are right about using overloading not being necessary. But it would definitely be cool.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://947837]
[corenth]: Good morning (in my neck of the woods)
[Cosmic37]: hello perly kings and queens
[jedikaiti]: 'ello Monks
[Cosmic37]: and monkeys too...
[Cosmic37]: is anyone doing machine learning with Perl?

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2018-02-20 18:09 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (274 votes). Check out past polls.