New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"0" in boolean context evaluates to False #87
Comments
|
I daresay it's what I would expect according to the principle of least IIRC, in Perl5 there is "0 but true" that evaluates to a true value in Ciao, 2015-03-11 10:48 GMT+01:00 grondilu notifications@github.com:
|
|
+1 to the string "0" being True, and the number 0 being false. If I have a statement |
|
👎 for me. Testing a string for length is more explicit. Having different behavor based on the type of the argument would produce action-at-a-distance bugs that are much harder to nail down than the examples mentioned above. |
|
@zoffixznet what do you mean by "different behavor based on the type"? I mean, that is hat types are for, encapsulate behaviour. For me a |
|
@FROGGS , I gues it's a difference of backgrounds. I'm very new to Perl 6, but have been coding Perl 5 for years. For me, this code/result would be pretty weird, especially if that sub is from some module about whose innards I should not care. But you do bring up a good point that types encapsulate behaviour and the above should probably be |
|
Or even |
|
I'm changing my opinion to 👍 then. The bugs would still be possible, but for a different reason, and conceptually _string_ "0" is not false. |
|
I have very mixed feelings about this, coming from a Perl 5 background. I'm used to not caring about the data type (and having it be unknowable), and I have thought falsy "0" to be convenient. At the same time, I've been tripped up on bugs like the 0° temperature (even though real programmers use Kelvin). Anyway, changing Str.Bool in Rakudo only generates 5 spectest failures, 4 of which are direct tests of Additionally, Str.Bool seems about 9% faster without checking for "0" in my primitive A/B test. Common op, but marginal gain. |
|
This issue can be considered fixed: befb20c3bf Now we just need to align rakudo+roast. |
While looking at http://rosettacode.org/wiki/Temperature_conversion#Perl_6, it occurred to me that it's a bit unfortunate that the string "0" (and not the integer) evaluates to False. It's not much consistent either, considering that other strings of null numerical value (e.g. "-0" or "0+0i") evaluate to True.
If "0" was evaluating to True, the loop could be written:
with no risk of ending the program prematurely if the user enters "0".
See also IRC backlog of this day.
The text was updated successfully, but these errors were encountered: