gleam-lang/gleam

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

Fermée

#5 695 ouverte le 9 mai 2026

 (2 commentaires) (0 réaction) (0 personne assignée)Rust (960 forks)batch import
good first issuehelp wanted

Métriques du dépôt

Stars
 (21 417 étoiles)
Métriques de merge PR
 (Merge moyen 10j 19h) (69 PRs mergées en 30 j)

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.

Guide contributeur