0

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'
adon
  • 1
  • 2

1 Answers1

1

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
Marc Scholten
  • 1,351
  • 3
  • 5
  • Thank you so much for replying! If I add this instance in the Application/Helper/View.hs section, I get an error as the User is not defined. Where would I go to define it? Also, is there a way to get this to work by moving it to Web/Types.hs or is this just the easiest way to make the instance available in multiple views? Lastly, thank you for informing me about the dot notation! The code looks much cleaner! – adon May 22 '23 at 20:57
  • This error can be fixed by adding an `import Generated.Types` to the top of the file. You can also get this to work in `Web.Types`, but it's not the suggested way how it's typically done in IHP apps – Marc Scholten May 23 '23 at 21:34