Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > mind-numbing numification

Reply
Thread Tools

mind-numbing numification

 
 
Unknown Poster
Guest
Posts: n/a
 
      01-29-2004
X-No-archive: yes

This is perl, v5.6.1 built for MSWin32-x86-multi-thread
-----
package Rational;

use overload "0+" => \&float;

# There is a subroutine called float() that, well, returns a float.
------
use Rational;
..
..
..
#$fraction holds an object of type Rational
my $fsq = sqrt($fraction);
------------
perl complains:
Operation sqrt: no method found, argument in overloaded package Rational at ...

Since sqrt() takes a numeric argument, I expected the overloading of "0+"
to force float() to be called on the object $fraction, but it doesn't work.
The code "my $fsq = sqrt($fraction->float());" compiles OK.
Why isn't numification working in this case?
 
Reply With Quote
 
 
 
 
Jay Tilton
Guest
Posts: n/a
 
      01-29-2004
(Unknown Poster) wrote:

: X-No-archive: yes

Why? Yours is a good question.

: package Rational;
:
: use overload "0+" => \&float;
:
: # There is a subroutine called float() that, well, returns a float.
: ------
: use Rational;
: #$fraction holds an object of type Rational
: my $fsq = sqrt($fraction);
: ------------
: perl complains:
: Operation sqrt: no method found, argument in overloaded package Rational at ...
:
: Since sqrt() takes a numeric argument, I expected the overloading of "0+"
: to force float() to be called on the object $fraction, but it doesn't work.

Because sqrt() is itself an overloadable operator.

You can either let the overload pragma handle this on its own:

use overload
"0+" => \&float,
fallback => 1;


or, probably better, go ahead and overload sqrt:

use overload
"0+" => \&float,
"sqrt" => sub { sqrt($_[0]->float) };

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57