gleam-lang/gleam

Add better error message when pattern matching on string prefix and suffix

Open

Aperta il 9 mag 2026

Vedi su GitHub
 (2 commenti) (0 reazioni) (0 assegnatari)Rust (21.417 star) (960 fork)batch import
good first issuehelp wanted

Descrizione

When attempting to pattern match on both the suffix and prefix of a string, the error message could be friendlier.

For example, with

case "my string!" {
  "my " <> object <> "!" -> todo
  start <> "!" -> todo
}

The current error is:

error: Syntax error
  ┌─ /private/tmp/match_string_end/src/match_string_end.gleam:5:21
  │
5 │     "my " <> object <> "!" -> todo
  │                     ^^ I was not expecting this

Found `<>`, expected one of:
- `->`

Hint: Did you mean to wrap a multi line clause in curly braces?

Compare to the error if you don't try to match on the string's prefix:

error: Syntax error
  ┌─ /private/tmp/match_string_end/src/match_string_end.gleam:6:5
  │
6 │     start <> "!" -> todo
  │     ^^^^^ This must be a string literal

We can't tell what size this prefix should be so we don't know
how to handle this pattern.

If you want to match one character consider using `pop_grapheme`
from the stdlib's `gleam/string` module.

Thanks to @the_tank_man_ from the discord for suggesting this :]

Also, I can start working on this.

Guida contributor