by cardanome · 2y ago · 0 comments
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.
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.
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.
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).
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?
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.
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.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`
local x = false
print(x ?? "message was nil")
print(x or "message was nil")
which results in>false
>message was nil