Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: switch for statements #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dumpstring
Copy link

@checkraisefold
Copy link

another alternative could be something similar to rust match statement?

@nothing1649
Copy link

not really sure how i feel about using existing for/do keywords in this context, at first glance i thought that these were loops and not case statements

@checkraisefold
Copy link

Actually I feel like using for and do is very ambiguous; what happens if you want to run a for loop or use a do end inside the switch case? I think that becomes very hard to interpret, and maybe hard to parse. There definitely needs to be an alternative keyword here

@alexmccord
Copy link
Contributor

alexmccord commented Oct 28, 2024

Parsing a match or switch statement with the syntax one normally would expect, cannot be deterministic in Luau's current syntax. Nothing gives any piece of information implying that we're, with great certainty, parsing a match or whatever.

Copy link
Contributor

@alexmccord alexmccord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to give my opinions even though the parsing rule for this is nondeterministic in Luau.

end
for "b" do
-- Code for case "b"
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fallthrough semantics in a switch is considered a mistake, begs a question wrt scoping rules e.g. in case 1 that falls through to case 2, are locals introduced in case 1 also visible to case 2?

-- Code for case "b"
break
end
for "a", "b", "c" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads like it's for multiple returns. "a" | "b" | "c" would have been a better syntactic choice here.


## Motivation

The purpose of the `switch` statement is to simplify readability in cases where multiple branches depend on the value of one variable. Luau currently supports branching by using chains of `if`-`elseif`, but these may become wordy if multiple values are to be checked, or if fall-through behavior is desired. A `switch` syntax allows a simpler, easier to read structure.
Copy link
Contributor

@alexmccord alexmccord Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if fall-through behavior is desired

There are many options available to you for implementing explicit fallthrough semantics if it is desired, which is arguably better than the language construct doing it for you.

One model you can steal from is user input events, where a chain of functions are invoked with the same value. If the value matches some pattern, do something. Even if the handler has "handled" that input, they can still choose to pass through or sink the event. Sounds familiar?

type HandlerResult = "pass" | "sink" | nil -- nil == "pass"
type Handler<T> = (T) -> HandlerResult
type HandlerChain<T> = {Handler<T>}


## Alternatives

For instance, in the absence of a syntax for `switch`, developers depend on `if`-`elseif` chains: In the absence of a syntax for `switch`, developers have to depend on `if`-`elseif` chains, which often become cumbersome and harder to read with multiple values to check. To that effect, consider handling the fall-through behavior of `switch` statements. Here every condition is explicitly repeated; thus, code becomes more verbose and error-prone:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats the premise twice?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also argue that the switch version is more error-prone, because consequent cases may potentially have to deal with conditions from the antecedent cases, depending on fallthrough, which may not always exist syntactically, i.e. you may call some function that itself could then throw an error.

@deviaze
Copy link

deviaze commented Nov 5, 2024

Luau could benefit from an expressive, Rust-like match expression syntax, but I don't see this feature as proposed being helpful enough to provide benefit to Luau users. switch statements with fallthrough aren't a great part of C, so I don't think bringing them as-is is a good idea for Luau.

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

Successfully merging this pull request may close these issues.

5 participants