haskell / haskell/error-messages

`Parse error in pattern` for operator-style data constructors is vague

Open
#18 2 comments 5 reactions 0 assignees View on GitHub
status:Composing error message tool:GHC type:error-message
Dominant language
No language data
Stars
76
Forks
19
PR merge metrics
No merged PRs in 30d

Description

Consider the following GHCi session:

```
ghci> f x:xs = print(x, xs)

:162:1: error: Parse error in pattern: f
```

This is a function that a beginner I was talking to asked about. They (understandably) didn't understand what the issue was, or how to fix it. The error message doesn't help much here. It does mention something about a pattern, though.

The solution, of course, is to add parentheses around the pattern. ` f (x:xs) = print(x, xs)`. I think it would be an improvement to reflect this in the error message:

```
:162:1: error:
Parse error in pattern: f x:xs
Hint: Constructor patterns with arguments must be surrounded by parentheses
```

Note that this is not as bad in the case of non-operator constructors:

```
ghci> data Foo = Foo Int
ghci> f Foo x = x

:2:3: error:
• The constructor ‘Foo’ should have 1 argument, but has been given none
• In the pattern: Foo
In an equation for ‘f’: f Foo x = x
```

I think we'd still benefit from including the hint. I think it's worded loosely enough to be understood as a possible solution, not an absolute one, so even if they just forgot an argument it'd be fine. But I'm more concerned about the operator case.

I think good parse error messages are important for a smooth learning experience, and the state of parse error messages today is fairly poor in my opinion. It'd be nice to see more of these sorts of issues opened here, like #14 , for example.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.