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

Re: fixing smartmatch (again (still))

Thread Previous | Thread Next
From:
Abigail
Date:
August 29, 2012 01:40
Subject:
Re: fixing smartmatch (again (still))
Message ID:
20120829084349.GB5338@almanda
On Tue, Aug 28, 2012 at 06:02:33PM -0500, Jesse Luehrs wrote:
> On Tue, Aug 28, 2012 at 06:52:35PM -0400, David Golden wrote:
> > On Tue, Aug 28, 2012 at 5:06 PM, Jesse Luehrs <doy@tozt.net> wrote:
> > 
> > > I think people will have fewer reservations about accepting weird
> > > special cases like this for a keyword-based control structure
> > > (especially considering the precedent set by while (<>), etc) than for
> > > an infix operator (since it will be a weird special case either way -
> > > "why can i do '$foo ~~ 1', but not '$a = 1; $foo ~~ $a'?".
> > >
> > >
> > The opportunity for confusion exists either way.  My suggestion is to
> > change *one* thing -- smartmatch -- instead of *two* things -- smartmatch
> > and when.
> > 
> > Then all people have to know/relearn is the smartmatch table, not the
> > smartmatch table *and* the special case rules for when.
> 
> But this *is* a change to when, that's what I'm saying. From perlsyn:
> 
>     Exactly what the EXPR argument to "when" does is hard to describe
>     precisely, but in general, it tries to guess what you want done.
>     Sometimes it is interpreted as "$_ ~~ EXPR", and sometimes it does
>     not. It also behaves differently when lexically enclosed by a
>     "given" block than it does when dynamically enclosed by a "foreach"
>     loop.  The rules are far too difficult to understand to be described
>     here.  See "Experimental Details on given and when" later on.
> 
> For instance, currently "when ($a && $b)" means
> "when ($_ ~~ $a && $_ ~~ $b)".
> 


Yeah, but "when (2 && 3)" does *NOT* mean

    when ($_ ~~ 2 && $_ ~~ 3)

as '2 && 3' gets constant folded:

    $ perl -MO=Deparse -E 'given ($_) {when (2 && 3) {say}}'
    use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
    given ($_) {
        when (3) {
            say $_;
        }
    }
    -e syntax OK
    $


Which, IMO, is a misfeature of a (mis?)feature.


Abigail

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