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

Re: YA smartmatch proposal [ was: Re: smartmatch needs your eyes]

Thread Previous | Thread Next
Aristotle Pagaltzis
October 8, 2015 00:57
Re: YA smartmatch proposal [ was: Re: smartmatch needs your eyes]
Message ID:
* Dave Mitchell <> [2015-10-07 10:30]:
> I think 95% of what people actually *want* a smartmatch system for
> is so that they can write code of one of these three forms:

Mostly agree.

> Why get rid of the smartmatch operator? I've never really seen the
> point of it, apart from being something that exposes the underlying
> mechanism of when(), c.f. readline() for <>. If we do keep it, then it
> should have exactly the same semantics as I've described above for
> when().

Disagree. Plenty of times I’ve written interfaces that let the user say
watch to match, so I’ve written various ad-hoc incarnations of “if you
pass a string, it will be compared exactly, if you pass a regexp object
it will be matched, if you pass a sub it will be called and the return
value will be checked for truthiness”. It would be real nice if all of
that could be replaced with just “pass a smarmatch operand” and all the
code implementing this sort of thing could go away.

(I reserve comment on any specifics of your proposal; I only wanted to
address your premises.)

* Eirik Berg Hanssen <> [2015-10-07 20:25]:
> If you are doing away with polymorphism, why squeeze these three
> different semantics into a single keyword? […]
>         ncase (0)     {...}
>         scase ("foo") {...}
>         when (undef)  {...}
>         when (/foo/)  {...}

At that point would go for making `when` exactly like `if`, except with
an implicit trailing `break`, yielding

    when ($_ == 0)     {...}
    when ($_ eq "foo") {...}
    when (not defined) {...}
    when (/foo/)       {...}

The `ncase` keyword is nothing more than an unfamiliar spelling of `==`
on top of this `when`, since it’s all purely syntactically determined
anyway. And the familiar spelling of `==` is perfectly serviceable as
far as I’m concerned. Under this proposal I wouldn’t even special-case
smartmatch – it would require explicitly saying

    when ($_ ~~ $something) {...}

Many things in Perl already know to check $_ in absence of an explicitly
named variable, so the main difference between implicitly smartmatching
`when` and the just-an-`if`-with-implicit-break `when` is

    when (0)     {...}
    when (1)     {...}
    when ("foo") {...}
    when ("bar") {...}


    when ($_ == 0)     {...}
    when ($_ == 1)     {...}
    when ($_ eq "foo") {...}
    when ($_ eq "bar") {...}

Whereas, in contrast, stuff like this is a wash:

    when (undef) {...}
    when (/foo/) {...}


    when (not defined) {...}
    when (/foo/)       {...}

Aristotle Pagaltzis // <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About