Skip to content

Comments

Add optional chaining to Luau#171

Open
SeriWTR wants to merge 2 commits intoluau-lang:masterfrom
SeriWTR:syntax-optional-chaining
Open

Add optional chaining to Luau#171
SeriWTR wants to merge 2 commits intoluau-lang:masterfrom
SeriWTR:syntax-optional-chaining

Conversation

@SeriWTR
Copy link

@SeriWTR SeriWTR commented Feb 7, 2026

Summary

This RFC proposes adding an optional chaining operator (?.) to Luau to enable safe and concise access to nested table fields without requiring explicit nil checks.
Optional chaining short-circuits evaluation when the left-hand side is nil, returning nil instead of raising an error.

Motivation

Accessing deeply nested tables in Luau often requires verbose nil-checking patterns, which reduce readability and are easy to get wrong.

For example,

local languageData =
    langName
    and lang[langName]
    and lang[langName].keys

or Luau-equivalent:

local languageData =
    if langName and lang[langName] and lang[langName].keys
    then lang[langName].keys
    else nil

Design

With the optional chaining, the codes written above can be reduced to:

local languageData = lang[langName]?.keys

If lang[langName] evaluates to nil, the expression evaluates to nil without error.

Backwards Compatibility

?. operator is already invalid in Lua and Luau, making backwards-compatibility safe to implement.

Drawbacks

Optional chaining must respect existing __index and __newindex semantics.

@Kampfkarren
Copy link
Contributor

FYI this has been previously approved but reverted in the past because of concern that its behavior with Roblox Instances would be non obvious.

luau-lang/luau#501

@SeriWTR
Copy link
Author

SeriWTR commented Feb 7, 2026

@Kampfkarren that sucks. Because this feature would be great for non-instance objects. Because most of the time you'll still have to check typeof calls, but for tables? ?. would've been great.

@Kampfkarren
Copy link
Contributor

Kampfkarren commented Feb 7, 2026

FWIW I don't really agree with the reasoning anymore, but unless Luau team has changed their mind on this there's basically nowhere to go with it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants