Log In  

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.

P#116417 2022-08-28 19:59

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)

P#116419 2022-08-28 20:34

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.

P#116492 2022-08-29 13:18

> 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.

P#116494 2022-08-29 13:57 ( Edited 2022-08-29 14:00)

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.

P#116515 2022-08-29 18:53
1

the and-or trick works in all contexts, including inside a print call

P#116526 2022-08-29 20:28

@merwok, yeah, but you need to put the things inside a table and after that take them. Also, it's very implicit

P#116569 2022-08-30 09:11

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.

P#116570 2022-08-30 09:16
1

@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.

P#116571 2022-08-30 10:38

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

P#116572 2022-08-30 11:28 ( Edited 2022-08-30 11:29)

"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.

P#116576 2022-08-30 14:00

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.

P#116578 2022-08-30 14:26

"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.

P#116579 2022-08-30 14:33
1

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!
P#116581 2022-08-30 15:16

Considering LUA has it's own way to do this I would not fight the language itself...

P#116608 2022-08-30 19:13

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 06:06:26 | 0.046s | Q:30