[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: inherted overload & mixed type operations



On Mon, Jan 10, 2000 at 04:01:36PM -0500, Ilya Zakharevich <ilya@math.ohio-state.edu> wrote:
> > _No_ other language chooses overloaded functions randomly like perl ;*)
> So does not Perl.

Please note that I added a smiley after that sentence. Perl does exactly
that: it sometimes calls one operator, and on the next occasion the
other. While that might not be "random" in the truest sense of "random", I
call that "random".

> > But to be more helpful: C++ (for example) lets you overload based on both
> > types.
> Only if *you* know all the types in advance

Please explain: I (and nobody else, either) need not know *anything*
about the type I overload. Of course, the _semantics_ to apply for types
I have no knowledge about is rather limited, but that's exactly the same
situation as in perl....

The difference is that I can _still_ overload based on both specific
types, when I need it, so it really seems to me that C++ overloading is a
superset of the simple-minded (but still effective) perl model.

> Which you can do in Perl as well: put the logic for recognizing that the
> second argument is PDL::Complex into PDL::overloaded_multiply.

The problem is that there is no documented way to overwrite or enhance
overloaded methods from other modules. So the only way would be to
actually _modify_ the source of the PDL module. But that module should not
need to know anything about complex numbers. That's what "subclassing" and
"abstraction" means to me, at least.

Not that changing PDL source cannot be done, but it is kind of ugly in
that case, which you hopefully agree with.

But we have to seperate the above feature-request (it is only a feature
request) from the actual bug happening somewhere!

> > > If i() returns an overloaded object, the *-method for this object
> > > will be called.
> > No.
> ???

"No" = "No, this does NOT happen." Or only sometimes ;)

> > dies with an error message, since PDL's * is being called, while:
> Recompile Perl with -DDEBUGGING, and run with -Do option.

Just did so. Ran the following program with -Dto:

use PDL; 
use PDL::Complex;
$a = zeroes(50);
$sin = (i) * $a;

Here are the (for me) relevant parts of the output:

(p:4)   Overloaded operator `*':
        method for left argument found in package PDL::Complex
        (p:4)   nextstate
(and later)
(/usr/app/lib/perl5/PDL/Core.pm:345)    gv(PDL::Ops::my_biop1)
(/usr/app/lib/perl5/PDL/Core.pm:345)    entersub

So yes, the multiply from PDL::Complex is NOT being called (if you want
more detailed info just drop me a note). The interesting thing is, when I
add an eval-wrapped copy of line 4 before that line, then my operator is
being called ON THE SECOND TIME, but not the first time.

((eval 11):1)   pushmark
((eval 11):1)   const(PV("now in Cmul, "))
((eval 11):1)   pushmark
((eval 11):1)   gvsv(main::")
((eval 11):1)   gv(main::_)
((eval 11):1)   rv2av
((eval 11):1)   join
((eval 11):1)   Overloaded operator `""':

(I use an anonymous sub that prints "now in Cmul" for my '*'-operator)

-- 
      -----==-                                             |
      ----==-- _                                           |
      ---==---(_)__  __ ____  __       Marc Lehmann      +--
      --==---/ / _ \/ // /\ \/ /       pcg@opengroup.org |e|
      -=====/_/_//_/\_,_/ /_/\_\       XX11-RIPE         --+
    The choice of a GNU generation                       |
                                                         |


Follow-Ups from:
Christian Soeller <c.soeller@auckland.ac.nz>
Ilya Zakharevich <ilya@math.ohio-state.edu>
Ilya Zakharevich <ilya@math.ohio-state.edu>
References to:
Marc Lehmann <marc@gimp.org>
Ilya Zakharevich <ilya@math.ohio-state.edu>
Marc Lehmann <marc@gimp.org>
Ilya Zakharevich <ilya@math.ohio-state.edu>
Marc Lehmann <marc@gimp.org>
Ilya Zakharevich <ilya@math.ohio-state.edu>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]