Operator overloading revisited

S

Sisyphus

Hi,

Say I have 2 modules, called module_x and module_y, and that both
modules overload the * operator (with a subroutine named over_mul).

let's further assume that I run the following script:

use warnings;
use module_x;
use module_y;
my $x = module_x->new(10);
my $y = module_y->new(12);

my $z1 = $x * $y;
my $z2 = $y * $x;

It looks to me that the first multiplication will use the
module_x::eek:ver_mul subroutine, and that the second multiplication will
use the module_y::eek:ver_mul subroutine - and that there's nothing one can
do to change that behaviour - ie I can't write 'my $z = $x * $y;' and
"trick" it into using the module_y::eek:ver_mul subroutine.

Now that all seems quite sane to me, but I know from past experience
that doesn't mean that I've necessarily got it right.

Basically, all I'm asking is that someone let me know if I'm wrong.

Cheers,
Rob
 
A

Anno Siegel

Sisyphus said:
Hi,

Say I have 2 modules, called module_x and module_y, and that both
modules overload the * operator (with a subroutine named over_mul).

let's further assume that I run the following script:

use warnings;
use module_x;
use module_y;
my $x = module_x->new(10);
my $y = module_y->new(12);

my $z1 = $x * $y;
my $z2 = $y * $x;

It looks to me that the first multiplication will use the
module_x::eek:ver_mul subroutine, and that the second multiplication will
use the module_y::eek:ver_mul subroutine - and that there's nothing one can
do to change that behaviour - ie I can't write 'my $z = $x * $y;' and
"trick" it into using the module_y::eek:ver_mul subroutine.

Now that all seems quite sane to me, but I know from past experience
that doesn't mean that I've necessarily got it right.

Basically, all I'm asking is that someone let me know if I'm wrong.

Ah... Cross-class overloading. A lot of fun can be had with that.

Basically, you're right. If the classes know nothing about each other,
the behavior you describe is what you get. If both operands overload
an operator, the left operand wins out. The name of the implementing
method is not relevant here, btw.

If the classes are allowed to know about each other, the overload
routine can check its second operand and implement a different
behavior.

Anno
 
S

Sisyphus

Anno said:
Ah... Cross-class overloading. A lot of fun can be had with that.

Yes - and that probably includes "fun" in the sense of hair removal :)
Basically, you're right. If the classes know nothing about each other,
the behavior you describe is what you get. If both operands overload
an operator, the left operand wins out. The name of the implementing
method is not relevant here, btw.

Yep - that's mainly what I was wanting to know.
If the classes are allowed to know about each other, the overload
routine can check its second operand and implement a different
behavior.

Haven't thought much along those lines ... but it's worth some
considerstion, at least.

Thanks Anno.

Cheers,
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top