develooper Front page | perl.perl5.porters | Postings from August 2009

Re: Preliminary report of smoking perl 5.10.1

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
August 20, 2009 12:26
Subject:
Re: Preliminary report of smoking perl 5.10.1
Message ID:
20090820192629.GK9886@iabyn.com
On Thu, Aug 20, 2009 at 07:39:03PM +0100, Dave Mitchell wrote:
> On Thu, Aug 20, 2009 at 01:55:11PM +0100, Dave Mitchell wrote:
> > Looking at the output, I see the basic changes in behaviour between 5.10.0
> > and 5.10.1, given the following code, are:
> > 
> > 
> >     package My::Class;
> >     use overload '~~' => 'matches', '""' => 'to_string';
> > 
> >     ...
> > 
> >     my $obj = My::Class->new;
> >     given ($obj) {
> > 	...
> > 
> > Then the following when()s make the overload calls shown below.
> > 
> > 
> >     when ( [1, 2, 3, 4] )
> > 
> > 	5.10.0:
> > 
> > 	       matches($obj, [ 1, 2, 3, 4 ], ''); 
> > 
> > 	5.10.1:
> > 
> > 	       matches($obj, 1, ''); 
> > 	       matches($obj, 2, ''); 
> > 	       matches($obj, 3, ''); 
> > 	       matches($obj, 4, ''); 
> > 
> > 
> >     when ( {a=>1, b=>2}  )
> > 
> > 	5.10.0:
> > 
> > 	       matches($obj, { 'a' => 1, 'b' => 2 }, ''); 
> > 
> > 	5.10.1:
> > 
> > 	       to_string($obj, 1, ''); 
> > 
> > 
> > Anyone on p5p care to comment on this changed behaviour? (Since
> > smartmatching isn't my area of expertise, and I've got to decide in the
> > next day or two whether to scrub the 5.10.1 release, and no-one else seems
> > to be saying anything....)
> 
> First off, with d7c0d2821cad1d2e5b6b3d6440e7c22bfaae9559 I've added
> a -DM debugging flag to track smart match resolution, eg:
> 
>     $ p -DM -E'1  ~~ "1"'
> 
>     EXECUTING...
> 
>     (-e:1)	Starting smart match resolution
>     (-e:1)	    applying rule Num-numish
> 
> Now need to play with it and think about the implications of what's
> actually happening.

Okay, what we seem to have here is an impedance match between the
perl6-ish design of ~~, where dispatch is based on the RHS, and the
perl5 expectation that overloading takes precedence over everything else.

eg in Perl 5, if $obj is an object with the appropriate overloading
defined, then we expect

    $obj <op> X

to always call

    $obj->some_method(X)

without caring what X is (for any suitable operator <op>).

Conversely, with

    $obj ~~ X

we dispatch mainly on the RH type, so for example

    $obj ~~ {qw(a 1 b 2)}

matches Any-Hash (not Object-Any), and does the equivalent of

    exists {qw(a 1 b 2)}->{$obj}

(with $obj's stringify method called), rather than

    $obj->some_method({ qw( a 1 b 2) });




-- 
"Emacs isn't a bad OS once you get used to it.
It just lacks a decent editor."

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About