Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua operators, why isn't +=, -= and so on defined?

This is a question I've been mildly irritated about for some time and just never got around to search the answer to.

However I thought I might at least ask the question and perhaps someone can explain.

Basically many languages I've worked in utilize syntactic sugar to write (using syntax from C++):

int main() {     int a = 2;     a += 3; // a=a+3 } 

while in lua the += is not defined, so I would have to write a=a+3, which again is all about syntactical sugar. when using a more "meaningful" variable name such as: bleed_damage_over_time or something it starts getting tedious to write:

bleed_damage_over_time = bleed_damage_over_time + added_bleed_damage_over_time  

instead of:

bleed_damage_over_time += added_bleed_damage_over_time 

So I would like to know not how to solve this if you don't have a nice solution, in that case I would of course be interested in hearing it; but rather why lua doesn't implement this syntactical sugar.

like image 800
qrikko Avatar asked Nov 20 '13 09:11

qrikko


People also ask

What does -= mean in Lua?

3.2 – Relational Operators The operator == tests for equality; the operator ~= is the negation of equality. We can apply both operators to any two values. If the values have different types, Lua considers them different values. Otherwise, Lua compares them according to their types.

Can you do += in Lua?

I've always found it odd that Lua doesn't include +=, ++, -=, etc. I understand that the language is meant to be small and streamlined, but it seems like something that would be fairly minor to add and would save a lot of time.

What is the OR operator in Lua?

Introduction to Lua or. The Lua or operator is a logical operator who connects more than two conditions and declares the true expression. The logical function is to declare the Boolean value of the two or more than two operands in the source code.


2 Answers

This is just guesswork on my part, but:

1. It's hard to implement this in a single-pass compiler

Lua's bytecode compiler is implemented as a single-pass recursive descent parser that immediately generates code. It does not parse to a separate AST structure and then in a second pass convert that to bytecode.

This forces some limitations on the grammar and semantics. In particular, anything that requires arbitrary lookahead or forward references is really hard to support in this model. This means assignments are already hard to parse. Given something like:

foo.bar.baz = "value" 

When you're parsing foo.bar.baz, you don't realize you're actually parsing an assignment until you hit the = after you've already parsed and generated code for that. Lua's compiler has a good bit of complexity just for handling assignments because of this.

Supporting self-assignment would make that even harder. Something like:

foo.bar.baz += "value" 

Needs to get translated to:

foo.bar.baz = foo.bar.baz + "value" 

But at the point that the compiler hits the =, it's already forgotten about foo.bar.baz. It's possible, but not easy.

2. It may not play nice with the grammar

Lua doesn't actually have any statement or line separators in the grammar. Whitespace is ignored and there are no mandatory semicolons. You can do:

io.write("one") io.write("two") 

Or:

io.write("one") io.write("two") 

And Lua is equally happy with both. Keeping a grammar like that unambiguous is tricky. I'm not sure, but self-assignment operators may make that harder.

3. It doesn't play nice with multiple assignment

Lua supports multiple assignment, like:

a, b, c = someFnThatReturnsThreeValues() 

It's not even clear to me what it would mean if you tried to do:

a, b, c += someFnThatReturnsThreeValues() 

You could limit self-assignment operators to single assignment, but then you've just added a weird corner case people have to know about.

With all of this, it's not at all clear that self-assignment operators are useful enough to be worth dealing with the above issues.

like image 54
munificent Avatar answered Oct 21 '22 13:10

munificent


I think you could just rewrite this question as

Why doesn't <languageX> have <featureY> from <languageZ>?

Typically it's a trade-off that the language designers make based on their vision of what the language is intended for, and their goals.

In Lua's case, the language is intended to be an embedded scripting language, so any changes that make the language more complex or potentially make the compiler/runtime even slightly larger or slower may go against this objective.

If you implement each and every tiny feature, you can end up with a 'kitchen sink' language: ADA, anyone?

And as you say, it's just syntactic sugar.

like image 35
Roddy Avatar answered Oct 21 '22 15:10

Roddy