develooper Front page | perl.perl5.porters | Postings from September 2015

Re: smartmatch needs your eyes

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
September 11, 2015 21:25
Subject:
Re: smartmatch needs your eyes
Message ID:
20150911212504.GA52829@plasmasturm.org
* H.Merijn Brand <h.m.brand@xs4all.nl> [2015-09-10 17:45]:
>     $x ~~ 42        -->     $x == 42        ???
>     $x ~~ "foo"     -->     $x eq "foo"     ???

In $x ~~ $y with $y eq 42 you cannot tell whether the user wanted the
smartmatch to mean $x == 42 or $x eq 42. You can only tell that when the
user has given you a literal 42 vs a literal '42'.

So this can only work with literals. Therefore it would appear to be
useless: $x ~~ 42 with a literal 42 will never mean anything other than
$x == 42, so you can just as well write $x == 42 yourself. And it gets
worse: $x eq 42 is shorter and easier to type than $x ~~ '42' !

However, there is one case for it: if `when` is defined in terms of
smartmatch, then these rules would allow writing e.g.

    given ($mode) {
        when ('enable')  { ... }
        when ('disable') { ... }
        # ...
    }

That’s syntax I don’t really want to lose, and it would be nicer to say
that `when (EXPR)` is sugar for `if ($_ ~~ EXPR) { ...; break }` instead
of making `when` have its own very special cases over and above what ~~
does.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

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