roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.46k stars 313 forks source link

Or in pattern match should be allowed in more locations #7073

Open bhansconnect opened 2 months ago

bhansconnect commented 2 months ago

We can match something like ("e", _) | ("exit", _) | ("q", _) | ("quit", _) -> but we should be able to compress that pattern and instead match: ("e" | "exit" | "q" | "quit", _) ->.

Sample code that should work:

app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br" }

import pf.Stdout
import pf.Stdin

main = getChoice

options = ["parse", "validate", "analyze", "analyze and correct"]

menu =
    options
    |> List.mapWithIndex \option, index ->
        "$(Num.toStr (index + 1)). $(option)"
    |> Str.joinWith "\n"

getChoice =
    Task.loop {} \{} ->
        Stdout.line! "\nPlease enter a number for an option below:\n\n$(menu)"
        Stdout.write! "\n> "
        choice = Stdin.line!

        optionRes =
            choiceNum = Str.toU64? choice
            List.get options choiceNum

        when (choice, optionRes) is
            ("e" | "exit" | "q" | "quit", _) ->
                Task.ok (Done {})

            (_, Ok option) ->
                Stdout.line! option
                Task.ok (Done {})

            (_, _) ->
                Stdout.line! "\nOops, that's not an option."
                Task.ok (Step {})