gleam-lang/gleam

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

Open

#5,695 创建于 2026年5月9日

在 GitHub 查看
 (2 评论) (0 反应) (0 负责人)Rust (21,417 star) (960 fork)batch import
good first issuehelp wanted

描述

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.

贡献者指南