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

Re: fixing smartmatch just hard enough (and when, too)

Thread Previous | Thread Next
From:
Damian Conway
Date:
August 23, 2012 19:32
Subject:
Re: fixing smartmatch just hard enough (and when, too)
Message ID:
CAATtAp6YmxDZLQ4K41z09trKcRFYF096Zh9i8b1t3Z6i+xavLA@mail.gmail.com
Okay, in light of our further discussions, here's a re-updated proposal.

Note that I have incorporated what I am now thinking of as
"the Golden rule", namely that an LHS overload fires first,
if both sides are overloaded. But I'm reserving the right to not
actually care which order the first two cases are tested for. ;-)

I have also added in two overloading cases that we seemed
to have so far ignored: for unambiguous Num and Str.

And I'm still hoping that the final fallback case might return undef
(not "") so as to differentiate "unsmartmatchable" from
"smartmatchable, but didn't match".

Damian

______________________________________________________________________

Smartmatch (~~) table
=====================

    $a       $b                    Meaning
    =====    ===================   ========================
    ~~-ol    Any             [1]   $a->ol_method($b)
    Any      ~~-ol           [1]   $b->ol_method($a, 'rev')
    Any      undef                 ! defined $a
    Any      CodeRef, &{}-ol       $b->($a)
    Any      Regexp,  qr-ol        $a =~ $b
    Any      Num,     0+-ol  [2]   $a == $b
    Any      Str,     ""-ol  [3]   $a eq $b
    Any      Any                   undef (with fatalizable warning)


    [1]  Includes junctions, whose overloading distributes the
         smartmatch over their elements in their several ways.


    [2]  $b is smartmatched as a Num only if it is unambiguously numeric.
         That is, if and only if at least one of the following is true:

             a.  $b contains an object with overloaded numerification,
                 but no overloaded stringification;

             b.  $b has an internal numeric representation
                 but no internal string representation;

             c.  some (as-yet theoretical) internal flag indicates that
                 $b originally had only an internal numeric representation.


    [3]  $b is smartmatched as a Str only if it is unambiguously stringy.
         That is, if and only if at least one of the following is true:

             a.  $b contains an object with overloaded stringification,
                 but no overloaded numerification;

             b.  $b has an internal string representation
                 but no internal numeric representation;

             c.  some (as-yet theoretical) internal flag indicates that
                 $b originally had only an internal string representation.

______________________________________________________________________

The when construct
==================

    Form                  Meaning
    ==================    ==================================
    when (EXPR)  {...}    if ($_ ~~ (EXPR))     {...; break}
    when {BLOCK} {...}    if ($_ ~~ sub{BLOCK}) {...; break}


    No exceptions.
    No special cases.
______________________________________________________________________

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