From 025c9fb17e9b06d44b9fb4b9536cc5cad60d46b0 Mon Sep 17 00:00:00 2001 From: dumpstring <100595391+dumpstring@users.noreply.github.com> Date: Sun, 27 Oct 2024 01:41:42 +0200 Subject: [PATCH 1/3] Create switch-for-statements.md --- docs/switch-for-statements.md | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 docs/switch-for-statements.md diff --git a/docs/switch-for-statements.md b/docs/switch-for-statements.md new file mode 100644 index 00000000..99ea0b06 --- /dev/null +++ b/docs/switch-for-statements.md @@ -0,0 +1,55 @@ +# switch for statements + +## Summary + +This RFC proposes a new `switch` syntax for the Luau language, adding `switch` statements for clearer, more concise branching logic. + +## 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. + +## Design + +The syntax below, now proposed, is using `switch value`, then the definition of cases with `for` blocks. Each case can take a tuple of comma-separated values; its code is inside `do` and `end`. A default case is a `do` block at the end. + +Example: + +```lua +switch value + for "a" do + -- Code for case "a" + end +for "b" do + -- Code for case "b" + break + end + for "a", "b", "c" do + -- Code for any of these values + break + end + do +-- Code for default case + end +end +``` + +## Drawbacks + +This syntax adds a pattern not found in Luau, and may necessitate concepts for developers to learn. The syntax looks like the syntax of `for` loops but isn't a loop, which can be confusing in some cases. + +## 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: ```lua if value == "a" then +-- Code for case "a" +elseif value == "b" then +-- Code for case "b" +elseif value == "c" or value == "d" then +-- Code for case "c" or "d" +else +-- Default case +end + +``` + +Using the above chained `if`s, the convenience a `switch` provides is consumed by repeated comparisons and explicit coding of fall-through behavior. This can result in longer branching code and is more annoying to maintain when cases are added or altered. +``` From 08a77449a94618f1fd37d4afc5ded25619f45b5d Mon Sep 17 00:00:00 2001 From: dumpstring <100595391+dumpstring@users.noreply.github.com> Date: Sun, 27 Oct 2024 01:45:16 +0200 Subject: [PATCH 2/3] fixed broken markdown. --- docs/switch-for-statements.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/switch-for-statements.md b/docs/switch-for-statements.md index 99ea0b06..698566f0 100644 --- a/docs/switch-for-statements.md +++ b/docs/switch-for-statements.md @@ -19,7 +19,7 @@ switch value for "a" do -- Code for case "a" end -for "b" do + for "b" do -- Code for case "b" break end From bbcfe9079e8db8561edf5b609e70316ecc979c08 Mon Sep 17 00:00:00 2001 From: dumpstring <100595391+dumpstring@users.noreply.github.com> Date: Sun, 27 Oct 2024 01:46:19 +0200 Subject: [PATCH 3/3] fix the markdown again. --- docs/switch-for-statements.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/switch-for-statements.md b/docs/switch-for-statements.md index 698566f0..ece589b1 100644 --- a/docs/switch-for-statements.md +++ b/docs/switch-for-statements.md @@ -39,7 +39,9 @@ This syntax adds a pattern not found in Luau, and may necessitate concepts for d ## 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: ```lua if value == "a" then +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: + +```lua if value == "a" then -- Code for case "a" elseif value == "b" then -- Code for case "b" @@ -48,8 +50,6 @@ elseif value == "c" or value == "d" then else -- Default case end - ``` Using the above chained `if`s, the convenience a `switch` provides is consumed by repeated comparisons and explicit coding of fall-through behavior. This can result in longer branching code and is more annoying to maintain when cases are added or altered. -```