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

Selecting a nonexistent field using record dot yields error about missing HasField instance #539

Open
MorrowM opened this issue Dec 30, 2022 · 4 comments

Comments

@MorrowM
Copy link
Contributor

MorrowM commented Dec 30, 2022

Using GHC 9.4.4:

λ> :set -XOverloadedRecordDot
λ> data Person = Person { name :: String, age :: Int } deriving (Eq, Show, Ord)
λ> john = Person "John" 34
λ> john.address

<interactive>:4:1: error:
    • No instance for (GHC.Records.HasField "address" Person a0)
        arising from selecting the field ‘address’
    • In the expression: john.address
      In an equation for ‘it’: it = john.address

While this is technically a "correct" error message I believe we can do better. For example

<interactive>:4:1: error:
    • Cannot select nonexistent field ‘address’
        from a record of type ‘Person’
    • In the expression: john.address
      In an equation for ‘it’: it = john.address

Whether to mention HasField at all is perhaps a question, but I believe that if a user is advanced enough to potentially want to mess with HasField then they don't need the error message to spell that out for them.

@dixonary
Copy link

Seconding this. It's severe enough that I really think it will slow down the take-up of record dot syntax, especially among new learners.

Anything of the general shape that Morrow suggests would be streets ahead.

@Kleidukos
Copy link
Member

Agreed, we can score big time with this feature amongst new users and this needs to be a prime concern.

@MorrowM
Copy link
Contributor Author

MorrowM commented Dec 30, 2022

The relevant GHC issue:
https://gitlab.haskell.org/ghc/ghc/-/issues/22382

@massung
Copy link

massung commented May 7, 2024

I know this issue is old, but wanted to add that - as a hobbyist Haskell user - this error popped up and it took me way too long to figure out that the actual issue was I was missing an import; the record wasn't in scope.

No instance for ‘GHC.Records.HasField
                         "login" Persist.User.User String’
        arising from selecting the field ‘login’

Having the error message be as suggested in the issue - and perhaps adding a little hint that the record type may not be in scope (not imported or misspelled, as with other error messages), would be very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants