Page 1 of 1

Feature Request: Add Optional Chaining (?.) Operator to Factorio Lua

Posted: Fri Nov 29, 2024 9:15 am
by plexpt
I would like to request the addition of the optional chaining (?.) operator in the Lua environment used by Factorio. This feature, allows for safe property access on potentially nil values, helping to simplify and streamline code when dealing with deeply nested objects.

It would be a great addition to the scripting environment, making code more concise and reducing the need for multiple if checks for nil values. It would also improve error handling and code readability.


an example demonstrating the difference between using the `?.` (optional chaining) operator and not using it.
Without Optional Chaining:
in environments like Factorio where `?.` is not available, you need to manually check if each part of the chain is `nil` before accessing properties.

Code: Select all

-- Without optional chaining
local main_frame
if player and player.gui and player.gui.screen then
main_frame = player.gui.screen.main_frame
end

if main_frame then
-- Do something with main_frame
else
-- Handle the case where main_frame is nil
end


Here, we need to explicitly check each level (`player`, `player.gui`, and `player.gui.screen`) for `nil` before trying to access `main_frame`. If any of these is `nil`, accessing `main_frame` would result in an error.
With Optional Chaining (?.):
With the `?.` operator, this process becomes much more concise. You don't need to manually check each layer for `nil`, as the operator automatically returns `nil` if any part of the chain is `nil`.

Code: Select all

-- With optional chaining
local main_frame = player?.gui?.screen?.main_frame

if main_frame then
    -- Do something with main_frame
else
    -- Handle the case where main_frame is nil
end
In this case, if `player`, `player.gui`, or `player.gui.screen` is `nil`, `main_frame` will automatically be set to `nil` without throwing an error. This makes the code much cleaner and more readable, especially for deeply nested structures.
Summary:
Without ?. : You need to manually check each level of the object chain to prevent errors.
With ?. : The operator handles nil checks automatically, simplifying the code and improving readability.

Re: Feature Request: Add Optional Chaining (?.) Operator to Factorio Lua

Posted: Fri Nov 29, 2024 9:33 am
by curiosity
plexpt wrote: Fri Nov 29, 2024 9:15 am This feature, introduced in Lua 5.4
There is no such feature in Lua 5.4.

Re: Feature Request: Add Optional Chaining (?.) Operator to Factorio Lua

Posted: Fri Nov 29, 2024 12:01 pm
by plexpt
This is indeed optional chaining in LUA . Some extra LUA lib adds this feature to this. So I would be nice to have an option where you can allow for optional chaining to be a thing.

Maybe in 2.1?

Re: Feature Request: Add Optional Chaining (?.) Operator to Factorio Lua

Posted: Fri Nov 29, 2024 12:05 pm
by IsaacOscar
Can someone provide a source for this?
I found no mention of it in https://www.lua.org/manual/5.4/manual.html

Re: Feature Request: Add Optional Chaining (?.) Operator to Factorio Lua

Posted: Fri Nov 29, 2024 12:15 pm
by plexpt

Re: Feature Request: Add Optional Chaining (?.) Operator to Factorio Lua

Posted: Fri Nov 29, 2024 12:18 pm
by posila
plexpt wrote: Fri Nov 29, 2024 12:01 pm This is indeed optional chaining in LUA . Some extra LUA lib adds this feature to this. So I would be nice to have an option where you can allow for optional chaining to be a thing.
Is THIS your source?
https://github.com/LuaLS/lua-language-server/issues/2076#issuecomment-1518826692 wrote: This is indeed optional chaining in LUA . For example in FiveM if you develop there in LUA it allows for optional chaining. Some extra LUA lib adds this feature to this. So I would be nice to have an option where you can allow for optional chaining to be a thing.

Re: Feature Request: Add Optional Chaining (?.) Operator to Factorio Lua

Posted: Fri Nov 29, 2024 1:42 pm
by posila
Anyway, I think we are focusing on wrong thing here.
The request for a new operator is valid regardless of whether the operator is supported by newer version of Lua or not.

Re: Feature Request: Add Optional Chaining (?.) Operator to Factorio Lua

Posted: Fri Nov 29, 2024 3:23 pm
by MaxAstro
This would definitely be super convenient if it existed. Giant walls of nil checks looks ugly and I almost always forget one...