Re:Deno Is Webby
by SonOfLilit on Wednesday March 16, 2022 @06:00PM
(#30704683)
Not GP, but I really like [] being false in ruby and python because I often want to ask "is this variable that should hold a collection holding a collection of things, or is it empty/false?"
[ Parent
| on HN ]
Why an array with zero element should be false? It doesn't make a lot of sense. And writing if (array.length > 0) is more explicit than writing if (array) anyway. Even in python, I prefer to be explicit and say if len(array) > 0 than to rely on implicit conversion rules.
It would be better if Javascript had Array.empty() instead of comparing array length. It would make things even more explicit and more convenient.
Re:Deno Is Webby
by marcosdumay on Wednesday March 16, 2022 @08:10PM
(#30705875)
Personally, I don't like if even accepting things that are not boolean. But an empty array being false makes exactly as much sense as an empty string being false, and both should have always the same behavior.
If you want objects to behave like dictionaries, an empty object should also behave the same, by the way.
Re:Deno Is Webby
by heavyset_go on Wednesday March 16, 2022 @10:22PM
(#30706888)
I'd argue that `if empty_list` is already explicit in Python.
[] is truthy in Ruby but there is #empty? on collection types. Rails also has #blank? which additionally works on false and nil.
[] is truthy in Ruby. Only false and nil are falsy.
Yeah, sadly zero is not falsy in Ruby.
I always found it strange that in Python, stuff doesn't get implicitly cast to string (even Java does that!), but for whatever reason the idiomatic way to check if a collection is empty is to do "if collection:", implicitly casting to bool.
Re:Deno Is Webby
by heavyset_go on Wednesday March 16, 2022 @10:27PM
(#30706932)
Nothing gets casted to a bool in Python if statements.
Re:Deno Is Webby
by hyperhopper on Wednesday March 16, 2022 @06:52PM
(#30705177)
Luckily, most languages have a `.empty()` method! You should use that instead for readability purposes.
Re:Deno Is Webby
by derekbaker783 on Wednesday March 16, 2022 @09:33PM
(#30706493)
And in that vein, JS has some().
Re:Deno Is Webby
by dorianmariefr on Thursday March 17, 2022 @08:42AM
(#30710498)
You are thinking about `[].present?` I think