On Tue, Mar 18, 2025 at 4:51 PM Mike Schinkel <m...@newclarity.net> wrote:
>
> While working on a parser, I've repeatedly encountered patterns like:
>
>    if c == ' ' || c == '\t' { /* handle whitespace */ }
>    if token == EOL || token == EOF { /* handle end markers */ }
>    if lexer.Type == TextLine || lexer.Type == WhitespaceLine { /* handle 
> lines */ }
>
> Go already allows multiple values in case statements:
>
>    switch c {
>    case ' ', '\t':
>       /* handle whitespace */
>    }
>
> I'm wondering if there could be a more elegant way to express this pattern in 
> Go, perhaps something conceptually similar to:
>
>    if c [some_syntax] ' ', '\t' { /* handle whitespace */ }
>
> Do you think that the Go team would be likely to consider such a proposal, or 
> would taking the time to prepare and submit a proposal likely just be for 
> naught?
>
> If this seems worthy of a proposal, what syntax do you think might be the 
> best fit for the Go language?

We can already write

func Is[T comparable](s T, vals ...T) bool {
    for _, v := range vals {
        if s == v {
            return true
        }
    }
    return false
}

and then we can write

    if Is(c, ' ', '\t') {
        ...
    }

So I don't see a big need for additional syntax here.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcV1YH9jV4y%2BRYe5%3DUB5jNk6iGhMnxc0orxVZixVFYUC1Q%40mail.gmail.com.

Reply via email to