Hi, I think the tittle is self explanatory. I always had to fight against that lua lack. I came from c, so I am very accustomed to use the inline conditions. And it can be very useful for shrink code, like with tweetcarts. I know, there are some approaches in lua to solve this, but an official and explicit way would be very good.
do you know about myval=cond and val1 or val2
? it's great. (although there is one gotcha to watch out for: if val1 is false or nil, the result will always be val2)
Yeah, I already saw that, but I don't like that method beacuse it's very implicit and have that type of fail.
A "a if cond else b" would be very cool.
> A "a if cond else b" would be very cool.
it's kinda here using the short-if syntax, the condition have to be in parenthesis :
a=true if(a) x="yes" else x="no" ?x a=false if(a) x="yes" else x="no" ?x |
output 'yes' then 'no'.
But I agree a true ternary operator would be insanely useful.
I already saw some people do that too, but the main problem of this is that you can't put it inside a print, for example.
the and-or trick works in all contexts, including inside a print call
@merwok, yeah, but you need to put the things inside a table and after that take them. Also, it's very implicit
A "a if c else b" works for all contexts and uses many tokens less than "(c and {a} or {b})[1]". And the first option is really more leggible.
@biteco8
That's not required at all. c and a or b
should work fine. Also I'm not sure why you're calling it implicit. The lua and
and or
operators are primarily defined for that usage.
It's required to work when a can be false or null. Bro, what you think it's clever, use and-or for inline conditions, or a ternary operator that every programmer already knows without the need to search in the documentation? Also, the code is more legibble with if-else than with and-or. I just see the ternary as an advantage
"Bro, what you think it's clever" <-- No? I was just pointing out that the Lua language is set up that way. Whether tutorials cover it is a separate matter, as is whatever other paradigms a programmer is used to.
That said, you're requesting a feature that's already covered by the language, but a version with one specific case covered in less tokens. I think your request would make a lot more sense if you showed why that specific case would come up. In particular, can you show an actual situation where both a and b could potentially be nil or false? If b could be, then there's no problem. If a could be, then you can write not c and b or a
instead.
Sorry, I am not native, I thought clever was relative a clear, but the translator disagree. I wanted to say that the ternary is clearer.
"c and a or b" requires you to think about what the line does at first. I was thinking that it can simply supply a easier reading for the beginners, as an equivalent way, but easier for we all.
you’ve made your request, no need to insist vehemently. we can discuss or disagree courteously!
if zep thinks this is a real problem and wants to diverge pico8-lua a bit more from stock lua 5.2, he may reply (although this thread is in the wrong category — you can change this).
some notes:
-
I and others were just pointing out the current way to do this.
-
putting the a/b parts inside a table is not common practice, because only false and nil evaluate to false in lua, not empty string or 0 or empty table. (it was different for python, which eventually added an inline ternary operator.)
- «requires you to think about what the line does at first» that’s true for all code
«a easier reading for the beginners» I don’t think there is anything intuitive in programming languages, it’s all constructed and has to be learned!
Considering LUA has it's own way to do this I would not fight the language itself...
[Please log in to post a comment]