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

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

Thread Previous | Thread Next
From:
Robert Sedlacek
Date:
August 16, 2012 15:43
Subject:
Re: fixing smartmatch just hard enough (and when, too)
Message ID:
1345156997.21691.17.camel@gomorrah
On Thu, 2012-08-16 at 17:58 -0400, Ricardo Signes wrote:
> So, last year I said "we really ought to cut most of smart match away."
> 
>   http://www.nntp.perl.org/group/perl.perl5.porters/2011/07/msg174272.html
> 
> I've slept on it about 380 times, and I'm still for it.  Here's the new plan:

That would be very nice.

>   (2) A simple scalar (non-reference, non-glob, non-vstring) on the rhs is
>   used for stringwise equality.  I have not yet seen any convincing argument
>   that we can correctly intuit an eq/== behavior, so we pick one.

Seems sensible. I'd also add an advantage of picking the string equality
test: Very often when I want to compare numbers, I don't just want to
compare equality. I want to compare ranges, if they are larger or
smaller, and so on. I don't do this with strings very often. I do use
string non-equality sometimes, but less often, and that would be solved
by `({...})` below without much overhead.

But of course that's just my use-case.

> I propose that `when(X)` *always* means `$_ ~~ X`.  All of the special cases
> for parsing and interpreting the inside of when can go.  They can all be
> reduced to `when(sub{...})`.  I would also smile upon `when({...})` acting as a
> test block.  (Since you can't put a hashref on the rhs of ~~ anymore, there is
> no ambiguity.)

That would make it certainly easier. With regard to `when ({...})`,
would that be a block as in `do {...}`? That would be nicely explicit
and efficient. I might for example not want to `when(sub { ... })` in a
high frequency loop. Being able to just have a block there would speed
that up.

The `when {...} {...}` variant proposed by David Golden would have the
advantage of being "explicitly special" and people might be more
discouraged trying to combine the `{...}` with, for example, junctions.

It'd also mean people having hash references there now can't be
surprised somehow.

> When ~~ works like this, I will use it all the time.

When I use ~~ now, I already use it mostly like you propose (except for
the new syntax bits).

regards,
-- 
Robert 'phaylon' Sedlacek

Perl 5 Consultant for
Shadowcat Systems Limited - http://shadowcat.co.uk/


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