Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve custom type pattern matching error message #4112

Open
bouzlibop opened this issue Dec 27, 2024 · 3 comments
Open

Improve custom type pattern matching error message #4112

bouzlibop opened this issue Dec 27, 2024 · 3 comments
Labels
good first issue Good for newcomers help wanted Contributions encouraged priority:medium

Comments

@bouzlibop
Copy link

Context

When trying to run following:

import gleam/io

pub fn main() {
  let X = 2
  io.debug(X)
}

in the playground

One gets:

error: Unknown variable
  ┌─ /src/main.gleam:4:7
  │
4 │   let X = 2
  │       ^

The name `X` is not in scope here.

error: Unknown variable
  ┌─ /src/main.gleam:5:12
  │
5 │   io.debug(X)
  │            ^

The name `X` is not in scope here.

Maybe this error message could be improved so that it's clear that here the user tried to pattern match on a constructor which does not exsits.

Notes

This issue was discussed on the Discord server:

Danielle — 11:09
Yeah that is a slightly confusing error message to me
lpil — 11:09
What you’ve written is valid Gleam
You’re pattern matching on a custom type constructor called X, but no such constructor is in scope
We couldn’t change the error message for this niche situation without making the error message for common situation worse
Danielle — 11:11
does it saying error: Unknown variable make the most sense considering it's a pattern match?
lpil — 11:12
Unknown constructor might be clearer
Unknown value?
bouzlibop — 11:14
"You’re pattern matching on a custom type constructor called X" would be a super helpful error message 🙂
lpil — 11:14
Could you make an issue with the above conversation please
bouzlibop — 11:14
Sure 👍
lpil — 11:17
Thank you

@GearsDatapacks
Copy link
Member

So what should the error messages be in these two cases?

@lpil
Copy link
Member

lpil commented Dec 27, 2024

I think "error: Unknown constructor" and "The custom type variant constructor X is not in scope here." could be clearer

@lpil lpil added help wanted Contributions encouraged good first issue Good for newcomers priority:medium labels Dec 28, 2024
@CloserToTheCenter
Copy link

CloserToTheCenter commented Jan 4, 2025

I suggest splitting between "no parameters case" and then "1+ parameters case".

  • For "1+ parameters", the current error message ("unknown variable") is sufficient. Notably, with some parameters written out, the user is probably trying to destructure, and has a constructor in mind.

  • For "no parameters", I am unsure of where destructuring to a parameter-less constructor would be useful. There's no variables to bind to. If someone has a useful example please share. This case deserves more attention.

Currently:

let X(text) = func()   // error: Unknown variable, The name `X` is not in scope here.
let Y = func()   // error: Unknown variable, The name `Y` is not in scope here.

Proposed:

let X(text) = func()   // keep the same
let Y = func()   // add -> "Hint: Variable names are typically lowercase, unless they reference a type."

Also possible: Change from "unknown variable" to "unknown constructor" for both cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Contributions encouraged priority:medium
Projects
None yet
Development

No branches or pull requests

4 participants