3 ms·
It’s been too long since I’ve played with Lua to recall whether this would also be the case, but in JS the nullish coalescing operator is useful over logical OR
by wk_end 3y ago
It’s been too long since I’ve played with Lua to recall whether this would also be the case, but in JS the nullish coalescing operator is useful over logical OR because it prevents false-y values (0, empty string, false) from being treated as nullish ones, a frequent source of bugs.
- cardanome 3y agoThe number 0 and empty strings are considered truthy, so it isn't that bad. return 0 or 42 > 0 Obviously the pattern doesn't work for checking for the existence of booleans as both nil and false are falsy but you are less likely to confuse that. So yes, the ?? would help a little bit but then again, you could just explicitly check for nil.
- adbachman 3y ago> the ?? would help a little bit that's the only point of the operator. given: a = 0; b = 42; then `a or b` and `a ?? b` give two different answers, but take the same number of characters to write. > just explicitly check for nil 1. I am lazy and 2. I am explicitly checking, that's why I picked `??` not `or`
- smegsicle 3y agofalse-y values in dynamically typed languages is a mistake
- nine_k 3y agoComing from Python, I find empty collections being falsey very convenient.
- durumu 3y agoI feel like typing len(...) is not that bad, compared to how verbose "default if var_to_test is None else var_to_test" is (compared to "var_to_test or default"). But I agree it is nice -- I wish Python had the ?? operator.
- viraptor 3y agolen() may be either expensive or not possible to calculate for some collections where an emptiness check is trivial. For example a proxy for results from some paged API where you hold the first page initially. Empty check is immediate, len() may take hours.
- smegsicle 3y ago(implicitly converting strings to numbers is pretty convenient too but that doesn't mean php made the right call)