On Sat, Jul 31, 2010 at 12:56 PM, David Green <david.gr...@telus.net> wrote:

> > On 2010-07-30, at 4:57 pm, Aaron Sherman wrote:
> >> given False { when True { say "True" } when False { Say "False" }
> default { say "Dairy" } }
> >> I don't think it's unreasonable to expect the output to be "False".
> >> However, it actually outputs "True". Why? Well, because it's in the spec
> that way. So... why is it in the spec that way?
>
> Well, if you want to do a boolean test, you'd probably use "if" instead;


I'm sorry, I didn't know I'd sent this message to the python-language list
;-)

Seriously though, I don't want "there's more than one way to do it" to be an
excuse for redundancy, but on the other hand, it's rather odd for a
rationale in Perl to be "there's already a way to do that" rather than the
intuitiveness of the feature, even when it's only situational.

It's also quite useful for testing truth as a fallback:

 given $_ {
   when $motor-oil { say "mmm syrupy!" }
   when $methane { say "dangerou!" }
   when $jet-fuel { say "I can haz skaiz?" }
   when /crude/ { say "refine it first" }
   when False { say "Sorry, no petroleum products" }
   when True { say "Unknown petroleum products" }
   default { say "Unpossible!" }
 }


but something that already gives you a Bool, like "when time>$limit", is
> likely to be the result you want to test itself rather than comparing it
> against $_ (which is likely not to be a Bool).
>

My problem with that is that it's a really odd use of given/when, and given
the implicit smart-match, it doesn't make much sense. Now, to slightly
backtrack, I do agree that there should be at least one way to do something,
and if that were the only way to perform independent tests within a given,
I'd agree. Thankfully, it's not:

 given $_ {
   when /clock/ { say "clock" }
   if time > $limit { say "tick" }
   default { say "tock" }
 }

And the really nice thing about that usage is that you immediately see that
we're not testing time with respect to $_, but with respect to $limit. If
you use when, that's left rather confusingly ambiguous unless you know that
boolean values are a special case.



> So Perl is trying to be helpful by doing something useful instead of making
> the useful thing much harder at the expense of something that isn't useful
> anyway.


Well, since it's easy to do both, as demonstrated above, I think we can
agree that we've satisfied the first rule.

But given isn't very interesting compared to smart matching in general, and
that's where:

 $foo ~~ True

Really does seem to me as a very intuitive question, and that question isn't
"is the RHS true?"



> The catch is that I think that comparing against a boolean IS useful. The
> fact that this question keeps coming up, even on the p6l list, seems to
> demonstrate that the "helpful" way isn't completely natural or obvious (at
> least, not to everyone).
>


Agreed.



-- 
Aaron Sherman
Email or GTalk: a...@ajs.com
http://www.ajs.com/~ajs

Reply via email to