More operators!

Well, I mean I know Lua doesn’t natively support classes but Moonscript lets you code as if it exists.

class Thing
  name: "unknown"

class Person extends Thing
  say_name: => print "Hello, I am #{@name}!"

with Person!
  .name = "MoonScript"
  \say_name!

A long, long time ago I actually added += style operators to Lua as part of a patch to make its syntax more like C/Javascript. Also like C, it makes assignment into an expression so you can write a = (b += c) if for some reason you think that’s a good idea.

Here’s a diff against Lua 5.1.4. Obviously not to be considered production ready, or really tested at all - there’s thing or two I can see that I’d change if I did it again. The key bit for the += operator is testassign.

So adding operators to Lua certainly wouldn’t be beyond the ROBLOX developers, especially considering they’ve implemented things like __namecall. I have my doubts it would be a good idea to spend the effort to make a change like adding operators that makes ROBLOX Lua so incompatible with vanilla though.

2 Likes

Does this really matter when they’re small quality of life improvements? Its not like it takes 5 seconds to learn Obj +=1 can’t be used and instead you gotta use Obj = Obj + 1 outside of ROBLOX Lua. On top of this the community has always held ROBLOX’s implementation of Lua as its own kind of thing this is why people called it Rbx.Lua.

I mean its one thing to add new features like official classes but this is simply comparable to syntactic sugar. A short-hand way of doing something.

Lua was made to be modded and changed. Lua developers know this. I don’t see why its bad to deviate from it especially if you aren’t dramatically changing the language.

For what it’s worth, those are just a facade for prototypes. You can do the same thing with stock Lua.

(on second thought, Lua is more powerful than Javascript in that respect because JS doesn’t allow operator overloading)

1 Like

It’s more that if you wrote code originally for ROBLOX but that’s applicable elsewhere (it’s definitely possible in a large project to have bits that don’t touch ROBLOX API) you’d have to modify it to use it in regular Lua. The maker of GMod apparently felt similarly, though.

It is suggested to avoid using the custom operators if you wish to use your scripts outside of Garry’s Mod, as they are incompatible with standard Lua.

He still added custom operators though with simply giving that notice. I think this is perfectly acceptable. ROBLOX can do the same.

That has more to do with Roblox being a unique environment than with any implementation differences from vanilla lua, which are really only the components regarding files, like os and io.

“–” normally starts a comment. This change could lead to confusing syntax errors. For example: “‘by’ is not expected.”

2 Likes

Huh? It has everything to do with how ROBLOX changed the language from simple additions like Vector2 to things like workspace.

Its everything that makes Roblox’s implementation of Lua different than vanilla.

Ditching ++ and – is fine since +1 or -1 you don’t really save any time unlike *= /= += -=
+= 1 serves ++ purpose fine

There could be some benefits here.

x[myFunction()] = x[myFunction()] + 1

Could have a different meaning from:

x[myFunction()] += 1

In the first case, myFunction must be called twice. Perhaps it uses math.random, or modifies global state, or makes use of HTTPService. The second case could be defined to require only one call to myFunction.

In languages like C# in these instances, x is only evaluated once. This would also be the expected behavior because the whole point of syntax like += is take the value you get and perform arithmetic onto it without having to pointlessly declare the value again.

If your function can return multiple different results and you want different results then it seems to be the best to have it explicitly shown for better readability.

That case is definitely where things get complicated

x[myFunction()] += 1

might result in

local v0 = myFunction()
x[v0] = x[v0] + 1

Although this might be safer, considering functions might cause x to change

local a = x
local v0 = myFunction()
a[v0] = a[v0] + 1

This case is a bit simpler

script.Folder.Module[myFunction()].SomeValue.Value += 1

might result in

local a = script.Folder.Module[myFunction()].SomeValue
a.Value = a.Value + 1

This is the simplest case

a.Value += 1

would result in

a.Value = a.Value + 1

This is why a “+=” syntax could be useful beyond syntactic sugar. Lua makes very few compile-time assumptions. The “+=” operator could create a compile-time assurance that it is safe to use the reference from the left hand side expression without re-evaluation to get the right hand side value.

1 Like

I ran luac earlier:

local x = {}                                                                                                                              
--    1     [1]     NEWTABLE        0 0 0                                                                                                 
                                                                                                                                          
x["test"] = 1                                                                                                                             
--    2     [2]     SETTABLE        0 -1 -2 ; "test" 1                                                                                    
                                                                                                                                          
x["test"] = x["test"] + 1                                                                                                                 
--    3     [3]     GETTABLE        1 0 -1  ; "test"                                                                                      
--    4     [3]     ADD             1 1 -2  ; - 1                                                                                         
--    5     [3]     SETTABLE        0 -1 1  ; "test" -                                                                                    
                                                                                                                                          
--    6     [3]     RETURN          0 1

It only got interesting when more table references or where more function calls were duplicated.

An opcode version would likely be something similar to: “MODIFYTABLE” followed by “ADD”. There are already instructions that are followed by JMP or a non-instruction. This only adds a few instructions to support all arithmetic reductions.

There may be differences in error cases and error reporting in some cases.

1 Like

Definition of compiler: a program that converts instructions into a machine-code or lower-level form so that they can be read and executed by a computer.

Lua source code is translated into Lua bytecode, a lower-level form in which the virtual machine interprets.
luac is a program explicitly defined as the “lua compiler” by lua themselves that can be used standalone

https://www.lua.org/manual/5.1/luac.html
Source code contains a lexer, parser, and a code gen https://www.lua.org/source/5.1/:
Lua 5.1.5 source code - llex.c
Lua 5.1.5 source code - lparser.c
Lua 5.1.5 source code - lcode.c

Compare it to Java
java → javac → java bytecode → java vm (let’s pretend we’re not using a JiT compiler)

I don’t know what the heck y’all are talking about, but Lua should definitely not be abused in this fashion. Might as well write our own language explicitly for Roblox if we start modifying Lua’s core features and syntax.

1 Like

Since when is adding syntactic sugar for a redundant coding pattern equivalent to writing your own language?
Thats not abuse.
Lua was intended to be modified.
I feel like people get on this out of hand slippery slope fallacy mindset.

Not everything is a slippery slope situation. I’m just saying that if we modify everything for the sake of convenience or to make it look pretty, we may as well just make our own language at some point.

This isn’t a necessary change, and so far the design philosophy for Roblox has been to not change things unless they’re necessary and/or in camelCase. It’s probably not gonna happen.

Not really. Lua is fine as is for the most part.
Give us moonscript as an alternative. Its a low effort lang, compiles to Lua.
They won’t update Lua because LuaJIT and porting efforts.