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

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

Thread Previous | Thread Next
Johan Vromans
August 30, 2012 01:15
Re: fixing smartmatch just hard enough (and when, too)
Message ID:
Peter Scott <> writes:

> Every hypothetical case I've seen involving wildly heterogeneous
> 'when' clauses in the same given block has been contrived
> example-ware. I don't see those in real life often enough to justify
> the burden that having ~~ in the language would exact in too many
> other cases.


Case (switch, select, ...) statements have always been intended to serve
two purposes: the programmer and the compiler. To the programmer it
provides a quick and hopefully elegant way to test something against
several constant values. Constant is important for the second purpose,
since it can be compiled into a very efficient branch table.

For its implementation in Perl5 I only see beneficial:

  for ( ... ) {         # or given
    when ( 404 ) { ... }
    when ( 501, 503, 777 ) { ... }
    ... otherwise ...


  for ( ... ) {         # or given
    when ( "head" ) { ... }
    when ( "p", "div", "article" ) { ... }
    ... otherwise ...

It would be very nice if it were possible to use use constant constants
but much to my astonishment these constants do not seem to be
sufficiently constant.

I see no real benefits in adding given/when as mere syntactic sugar
for chained if/then/else constructs. We already have enough of these.
As for using smartmatch, see below.

> I regret to say that I have come to think of it as the
> too-smart-for-its-own-good-match operator.


Smartmatch was a good idea but unfortunately it turns out to be a big
implementation/usage problem. Beyond repair, I'm afraid. 

-- Johan

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