Skip to content

if local statements and expressions - #238

Open
annieetang wants to merge 2 commits into
masterfrom
if_local
Open

if local statements and expressions#238
annieetang wants to merge 2 commits into
masterfrom
if_local

Conversation

@annieetang

@annieetang annieetang commented Aug 6, 2026

Copy link
Copy Markdown

status: implementing

Rendered

This RFC introduces the syntax if local, and is a tighter scoped version of #110! We'd love to start the work towards what was discussed there and think that a single binding per if is the minimal useful case and would still be valuable on its own.

@deviaze

deviaze commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

I would've scoped my RFC smaller if I were given actionable feedback and clearer communication to that end.

while local/const bindings are a major usecase, and previous discussion has held them as more important than if local/const; I don't feel they should be left for future work.

@Bottersnike

Copy link
Copy Markdown

When the parser sees local or const immediately after if or elseif, it parses:

('local' | 'const') Name [':' Type] '=' exp

[snip] Since neither local nor const can start an expression in Luau, there is no ambiguity

This is not true. const is a valid variable name and as such can absolutely be the start of an expression.

local const = true
if const then
  ...
end

Is legal code. This can probably be resolved with an additional lookahead as I don't think 'if' Name('const') Name is currently legal.

@annieetang

Copy link
Copy Markdown
Author

I would've scoped my RFC smaller if I were given actionable feedback and clearer communication to that end.

while local/const bindings are a major usecase, and previous discussion has held them as more important than if local/const; I don't feel they should be left for future work.

just recapping here from the discussion in OSS but we will (and always were going to!) list you as a co-author on this as well and really appreciate the discussions and ideas on this, and plan to keep working with you to extend this to more of the use cases that we've been discussing in other threads/rfcs 😊

@Vighnesh-V

Copy link
Copy Markdown

@deviaze I would agree with while local being an important part of the feature as a whole - it is something the team actively wants to deliver. I would disagree with it being more important that if local (anecdotally).

@deviaze

deviaze commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

@deviaze I would agree with while local being an important part of the feature as a whole - it is something the team actively wants to deliver. I would disagree with it being more important that if local (anecdotally).

Oh, I personally agree with that take too. I need if local much more than while local, but I've been told multiple times that

"if local doesn't clear the -100 points bar -- you can already do if local with do end or another line of code, whereas while local is the feature I genuinely need".

@deviaze

deviaze commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Intro should clarify that this feature only applies to if local statements; if local expressions are not in scope, and future work should include why if local expressions are significantly more complex (compiler work).

@Vighnesh-V

Copy link
Copy Markdown

While I personally agree with you and think that the consistent minimal feature set here should also include while, I don't think if local on it's own doesn't clear the -100 points bar. It is useful on its own, and its addition to the language would be a net positive for folks. I can support a tighter scoped version of this rfc because I think its a) extensible and b) delivers value pretty quickly for people.

@annieetang

Copy link
Copy Markdown
Author

Intro should clarify that this feature only applies to if local statements; if local expressions are not in scope, and future work should include why if local expressions are significantly more complex (compiler work).

Ah that's a good callout, thanks! I'm happy to extend the scope to supporting if local expressions, so something like

local y = if local x = foo() then x else "a"

should be supported. But I'd still want to keep while local for future work to avoid scope creeping, and also because if and while are different syntactic constructs.

@deviaze

deviaze commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Intro should clarify that this feature only applies to if local statements; if local expressions are not in scope, and future work should include why if local expressions are significantly more complex (compiler work).

Ah that's a good callout, thanks! I'm happy to extend the scope to supporting if local expressions, so something like

local y = if local x = foo() then x else "a"

should be supported. But I'd still want to keep while local for future work to avoid scope creeping, and also because if and while are different syntactic constructs.

Yeah I'd love if local expressions. I was just told they were much more significant work! I've wanted both. If possible, would the compiler work to unblock if local expressions also technically unblock binding declarations in expressions (for future RFCs on block expression syntax ala Rust)?

@vegorov-rbx

Copy link
Copy Markdown
Collaborator

I was just told they were much more significant work!

After we evaluated compiler closer, it is more work, but does not look significantly harder to avoid if we like the feature (same for block expressions).

@annieetang annieetang changed the title if local if local statements and expressions Aug 6, 2026
@annieetang

Copy link
Copy Markdown
Author

just updated to add if local expressions :)

@annieetang

Copy link
Copy Markdown
Author

hey everyone! let us know if you have further design comments about this. since it's been about 2 weeks, we're going to move on and start implementing this, but will leave the RFC open to keep the discussion open if there are new things that come up

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.

5 participants