How do I add instances to the Web/Types section?

41 Views Asked by At

Currently, I am making program to help book appointments. Part of that includes making timeslots. In my program I have created the following instance for a selectField option in a form

instance CanSelect User where
    type SelectValue User = Id User
    selectValue user = get #id user
    selectLabel user = get #displayName user

This is in Web/View/Timeslots/New.hs, but to limit the amount of duplication, I was advised to move this to Web/Types.hs. When I copy-paste this into Web/Types.hs, I get this error:

Web/Types.hs:17:10: error: Not in scope: type constructor or class `CanSelect' | 17 | instance CanSelect User where | ^^^^^^^^^

I have no idea how to fix this

Edit: I have fixed this by defining a class for CanSelect in the Web/Types.hs section, but have some type errors

class CanSelect model where
    type SelectValue model :: Id model
    selectLabel :: model -> Text
    selectValue :: model -> Id model

instance CanSelect User where
    type SelectValue User = Id User
    selectValue user = get #id user
    selectLabel user = get #displayName user

The error message reads:

  • Couldn't match kind *' with Id' "users"' Expected kind Id User', but Id User' has kind `*'
    • In the type Id User' In the type instance declaration for SelectValue' In the instance declaration for `CanSelect User'
1

There are 1 best solutions below

2
Marc Scholten On

This instance actually belongs to Application/Helper/View.hs. That file already imports the CanSelect class, so this should fix the issue.

Also regarding your code example: The latest IHP version now supports Haskell's new dot notation, so you can replace the get #something like this:

instance CanSelect User where
    type SelectValue User = Id User
    selectValue user = user.id
    selectLabel user = user.displayName