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

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

Thread Previous | Thread Next
From:
Peter Scott
Date:
August 31, 2012 15:52
Subject:
Re: fixing smartmatch just hard enough (and when, too)
Message ID:
50414027.2040807@PSDT.com
On 8/31/2012 2:56 PM, Ricardo Signes wrote:
> * Peter Scott <Peter@PSDT.com> [2012-08-31T16:04:53]
>
> FC's suggestion was that when have three meanigns:
>
>    when (NUM)  { ... } # numeric literal, means $_ == NUM
>    when (STR)  { ... } # string literal,  means $_ eq STR
>    when (EXPR) { ... } # other expression, evaluates normally as in "if"
>
> Smartmatch (~~) was to be abolished.
Thank you for the painstakingly patient and polite education.  Now light 
dawns.  Decoupling when and smartmatch.  Is that generally agreed now no 
matter what?

> On the other hand, eliminating the first two tokens in "$_ eq 'foo'" 
> or "$_ == 4" in each when statement like that is a win. 

I wonder if restoring just the operator part of that comparison would be 
a suitable compromise?  Because then - if if it can be parsed, and maybe 
it cannot, I have the bliss of ignorance there - I would like to write 
things like:

when ( < 65    ) { say "Cold"       }
when { == 98.4 } { say "Tasty"      }
when { >= 75   ) { say "Hot"        }
default          { say "Just right" }

and then the presence of == vs eq would disambiguate the type and I 
could use $BLOOD_TEMPERATURE etc.  [I also thought about 'when ( >= 65 
&& < 75)' but my ignorance isn't *that* blissful.]

> This means that ~~ has exactly four valid types of rhs, which you 
> agreed were simple and probably memorable.

Yes, they are brief enough to be memorized.  I just wonder whether they 
are coherent enough to make sense or cause a reaction of "Huh? Why not 
also...?"  Because the place that reaction would be most likely to cause 
damage is here, in three years, when this discussion has been forgotten 
enough for someone to start a "Adding functionality to ~~" thread.

> When has three behaviors: numeric literal, string literal, and 
> "anything else," where the final case is taken as the rhs to a 
> smartmatch, with the topic as the lhs. 

That last case is mooted for abolition though, right?

> I think this means your remaining objection is that it is too 
> confusing that these two things differ: when (42) { ... } $x = 42; 
> when ($x) { ... }

Yes.  If that happened I could only ever teach it with big bold warnings 
in front.  Probably not even then.

> Please let me know if there's more to your remaining objections than 
> that.

No.  It was the decoupling of smartmatch and when that I missed. Thank you!

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