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

Re: fixing smartmatch (again (still))

Thread Previous | Thread Next
From:
Jesse Luehrs
Date:
August 28, 2012 14:06
Subject:
Re: fixing smartmatch (again (still))
Message ID:
20120828210622.GY11137@tozt.net
On Tue, Aug 28, 2012 at 04:59:46PM -0400, David Golden wrote:
> On Tue, Aug 28, 2012 at 2:43 PM, Ricardo Signes
> <perl.p5p@rjbs.manxome.org>wrote:
> 
> >   given ($x) {
> >     when ($y)  { ... } # $x ~~ $y
> >     when (4)   { ... } # $x == 4
> >     when ('4') { ... } # $x eq 4
> >   }
> >
> > Deferred/computed values of any sort mean smart match.
> >
> > Two dead-simple special cases: Numeric literals mean ==.  String literals
> > mean
> > eq.
> >
> 
> I agree this solves many of the technical issues raised, but I fear that
> it's going to confuse some people or mis-set expectations.
> 
> * Currently when() does smartmatch, so you have to retrain people not to
> think of when() as smartmatch, but to think of when() as a "smart switch"
> or something.

To be fair, this isn't currently true at all - when is very much a
"smart switch". Consider the behavior of "when (qr/a/ && sub { ... })",
for instance.

> * You can test a value with when() that you can't smartmatch, so if people
> don't internalize that when() is no longer smartmatch, at some point
> they'll smartmatch a literal and expect it to work like when.
> 
> Since smartmatching a literal isn't ambiguous, I think that it would be
> less confusing to just add literals to the smartmatch table and let when()
> stay as smartmatch.  That means *one* big change instead of two.

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'?".

> FWIW, I would still like "when {...} {...}" as a shorter way to write "when
> (sub{...}) {...}", but think that's "nice to have" and not necessary for
> fixing smartmatch.

I think it's a relatively minor gain, for a larger cost in complexity.

-doy

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