Re: Replacing the "if" statement with an "if" expression which supersedes it
Francisco Olarte <[email protected]> Sat, 27 Jun 2026 20:10:42 +0200
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <CA+bJJbzM6xccCxt5dB+PHVubx_jauju-kqXgLpGnnR=kxvx2jA@mail.gmail.com> |
On Sat, 27 Jun 2026 at 19:29, Lan Krownet <[email protected]> wrote: > > Operator assignment is not necessarily the only way to do it, because the issue assignment operators are trying to solve is not having to retype a really long variable, well, you could have a single unary prefix operator with a number that basically takes the variable being assigned in the expression and pastes it into the code like IMO the retyping is not the problem. > areallylongvariable = $0+1 > turns into > areallylongvariable = areallylongvariable+1 This is some kind of preprocessor trick. Useful, but there are things like: f(a).x.g(b)[h(d+e)]*=2 where, due to potential side effects, you can not use it. And even without them you will hit a runtime penalty. I mean, in lua you cannot substitute a.b.c.d=a.b.c.d+1 with do local x = a.b.c x.d=x.d+1 end. So that rules out the preprocessor tricks and either imposes a runtime penalty or forces you to exactly define the semantics. In fact I fear this need is one of the arguments for not having op-assign. In C, where you have addresses, it is easier as you can define x+=1 as y=&x; *y=*y+1. But in lua = is a complex beast. Besides, coming from old assemblers, and C/C++/perl, I've found a+=7 is a more fundamental op than a=a+7, I'm used to do think on it as "add a,1" and "add_and_store a,b,1". In fact when you overload operators in C++ you normally do it that way. > or it could point to any section, which is nice when you just want to swap some variables > var1, var2 = $2, $1 -- variables are swapped Once you "preprocess" you can also macro that, that is why I like having preprocessors. You do not use much but when you have half a page of swaps they are nice and let you give names to things without cost. But I'm disegressing, so lets sign out. Francisco Olarte. -- You received this message because you are subscribed to the Google Groups "lua-l" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion visit https://groups.google.com/d/msgid/lua-l/CA%2BbJJbzM6xccCxt5dB%2BPHVubx_jauju-kqXgLpGnnR%3Dkxvx2jA%40mail.gmail.com.