4 ms·
The Nil-Coalescing Operator they add is actually redundant. Instead of their new fancy syntax: print(message ?? "The message was nil.") The idiomatic way w
by cardanome 3y ago
The Nil-Coalescing Operator they add is actually redundant.
Instead of their new fancy syntax:
print(message ?? "The message was nil.")
The idiomatic way would be:
print(message or "The message was nil.")
The or operator works just fine. (Edit: Note that this works because empty strings, lists and the number 0 are all truthy in Lua)
Generally not a big fan of adding additional syntax and operators for minimal gain.
I feel like most changes are nice to have but not really worth the disadvantage of not being able to use LuaJIT.
Honestly, the most interesting goal for me is is having a beefier Python-style standard library. Now, if we had that together with LuaJIT levels of performance that would be an absolute Python killer.
The only actual problem with Lua (that can't be fixed without forking) is that new variables are global by default but you can cope with that. Just have your linter check that you are always using the "local" keyword. Couldn't find if Pluto is fixing that.
Edit: Oh they provide a "let" alias for "local", case in point, little changes with not much value.
- DemocracyFTW2 3y agoNo 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)
- deleted 3y ago[deleted]
- wk_end 3y agoIt’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.
- tvb12 3y agoEven though it would make the language a little more verbose, I would love if Lua had a 'global' keyword and declaring global/local was required. With that one change, I think Lua would have the best implementation of scope out of any language I've used.
- thefaux 3y agoYes. My solution though is to just pretend that naked assignment isn't supported and don't use global variables.
- nmz 3y agoPenlight[1] was supposed to offer python like batteries. It's kind of been abandoned by now, as has most of lua IMV. To me you still need luastd and a couple of other things (posix, lfs, socket, luasec) you can just fetch with luarocks and you're semi set. [1]: https://lunarmodules.github.io/Penlight/ https://lunarmodules.github.io/Penlight/
- trynumber9 3y agoIt is actually not redundant. I have run into this problem at least once in real Lua code. local x = false print(x ?? "message was nil") print(x or "message was nil") which results in >false >message was nil