5 ms·
Can someone explain why the first one is a wat? To me it seems perfectly logical Cast false to a string, then check the Boolean value of that string. It's not
by jayflux 11y ago
Can someone explain why the first one is a wat?
To me it seems perfectly logical
Cast false to a string, then check the Boolean value of that string. It's not an empty string so of course it would be true.
- lostmsu 11y agoBecause people from normal langs or without cs background expect type cast roundtrip to return original value if temp type can represent any value of source type (e.g. string can represent any value of bool)
- StavrosK 11y agoSo someone would expect that an int cast of "2" would be 2? What would an int cast of "a" be?
- dalke 11y agoI'll add to that - In what "normal langs" is this not a problem? What would a C or C++ programmer expect? Because my experience in those languages says "casting" is a meaningless way to understand what's going on. Perl doesn't have bareword true/falue values. Ruby doesn't use "casting", I think. That is, I think the idiomatic way to express this in Ruby is: >> !!true.to_s => true >> !!false.to_s => true