3 ms·
No it's not the same; `x ?? 'foo'` evaluates to `x` when `x` is not `nil` and to `'foo'` otherwise; `x or 'foo'` evaluates to `x` when `x` is not one of `nil` o
by DemocracyFTW2 3y ago
No it's not the same; `x ?? 'foo'` evaluates to `x` when `x` is not `nil` and to `'foo'` otherwise; `x or 'foo'` evaluates to `x` when `x` is not one of `nil` or `false`, and to `'foo'` otherwise.
This idiom seems to have been made widely popular by Perl folks, and while it has always been a good trick up your sleeve when you're 16 and want to impress the other kids, it's never been a good, correct way of doing things seriously, especially not across different scripting languages when every single one has its own Byzantine rules of what counts as truthy and what as falsey (like empty strings or empty lists). It is an especially pernicious idiom because of the widespread clout with which it tends to be transmitted; turns out it's one of the Falsehoods Clever Programmers Think They Know Better About. They don't.
- ledauphin 3y agoi can see you are probably a polyglot, working in several languages with regularity. it would certainly be frustrating to have to juggle these semantics as you switched back and forth. however, languages are just that - they have their own meanings, and unfortunately they can't all be the same across languages (or they'd all be the same language).
- DemocracyFTW2 3y agodifferent languages are different, and they have every right to be different, and I have every right to point out in whichever ways they differ. Personal tastes differ, too, and as far as I'm concerned I think that using 'shorthand Boolean coalescence' (I just made up that term) is a false convenience; when I write a condition that relies on a list being empty, ideally that should look like `if is_empty a then ...` according to my taste—explicit is better than implicit, and this is still concise. Be that as it may but shrugging and sighing, like, "ah well, different languages will be different" and using that as an argument against a discussion of the merits and demerits of certain aspects of certain languages is not constructive, is it?
- naniwaduni 3y agoYou want to learn the specifics of ?? means in this language, but not the boolean projections? It's one thing to be peeved that empty strings or lists are falsy when it's useful for them not to be, but this seems like barking up the wrong tree.
- fieldcny 3y agoI’m not clear on what the point of your response is, the comment you responded to is making an often overlooked point that certain subtleties in how languages implement constructs can create sharp edges, one of those being the null coalescing operator.
- smegsicle 3y agothe point is that lua didn't make the mistake of false 'empty' values, so that particular sharp edge is absent
- DemocracyFTW2 3y agoThe edge of "false 'empty' values" is absent in Lua, indeed, but the `a = b or c`, more often used in the form `b = b or c`, has, in Lua, still the problem that it will evaluate to `c` if `b` is `nil` and when b is `false` which may or may not be what one wants—and often one wants it not. The idiom is commonly used to assign a proper value to a variable that has not been set to an explicit value, i.e. a variable that is `nil`. The assignment `b = b or true` will set `b` to `true` if it was `nil` or `false`; assuming allowable values for `b` are `true` and `false` and the default is `true`, `b` will then never keep the value of `false` and always be set to `true`.
- DemocracyFTW2 3y agoYou almost never want to exploit, in a scripting language, the subtleties of the Boolean projections when writing the (un-) idiom `b = b or b_default`; it is more commonly a mistake made by script kiddies and hand-waving mavericks (i.e. People Who Finish First, a.k.a. People Who Never Write Proper Tests) that has become deeply ingrained due to the Look Ma How Clever effect. If `b` should be a Boolean with a default of `true` then `b = b or true` will quite obviously never allow it to be `false`. Rewrite that and put it in a row of such assignments as in `a = a or a_default; b = b or b_default`; c or c or c_default;` and it looses its fishy smell enough to pass as "OK, trivially no problem here". I've indeed gotten tired by the entire idea of Boolean projections, yes, but that's not the point here, at least not almost always in practice.
- cardanome 3y agoEmpty strings, lists and the number 0 are all truthy in Lua so this isn't an issue. Different languages have different semantics. Yes, using the or operator like that is not a good idea in many languages but in Lua it is idiomatic.
- DemocracyFTW2 3y agopls see my other comment(s)
- awwaiid 3y agoWell the Perl people also invented the defined-or operator (//), so there is that.
- DemocracyFTW2 3y agoI was just guessing here and didn't know about `//`; this in turn comes transparently from C as an enhancement of the C idiom `b = b || c`; it may have been a later introduction though (me learning Perl was way back in the (early?) 90s) or I missed it (not unlikely)