-
Notifications
You must be signed in to change notification settings - Fork 39
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
base: master
Are you sure you want to change the base?
Conversation
another alternative could be something similar to rust match statement? |
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 |
Actually I feel like using |
Parsing a |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Luau could benefit from an expressive, Rust-like |
switch for statements