gleam-lang/gleam

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

Open

#5695 opened on May 9, 2026

View on GitHub
 (2 comments) (0 reactions) (0 assignees)Rust (21,417 stars) (960 forks)batch import
good first issuehelp wanted

Description

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.

Contributor guide