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

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
plexpt
Long Handed Inserter
Long Handed Inserter
Posts: 96
Joined: Mon Feb 07, 2022 2:32 am
Contact:

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

Post 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.
Last edited by plexpt on Fri Nov 29, 2024 3:00 pm, edited 2 times in total.
curiosity
Filter Inserter
Filter Inserter
Posts: 559
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

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

Post 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.
plexpt
Long Handed Inserter
Long Handed Inserter
Posts: 96
Joined: Mon Feb 07, 2022 2:32 am
Contact:

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

Post 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?
User avatar
IsaacOscar
Filter Inserter
Filter Inserter
Posts: 843
Joined: Sat Nov 09, 2024 2:36 pm
Contact:

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

Post 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
posila
Factorio Staff
Factorio Staff
Posts: 5439
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

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

Post 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.
posila
Factorio Staff
Factorio Staff
Posts: 5439
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

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

Post 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.
User avatar
MaxAstro
Inserter
Inserter
Posts: 32
Joined: Sun Nov 09, 2014 7:53 am
Contact:

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

Post by MaxAstro »

This would definitely be super convenient if it existed. Giant walls of nil checks looks ugly and I almost always forget one...
"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark Twain

"True friends stab you in the front." - Oscar Wilde
Post Reply

Return to “Modding interface requests”