Skip to content

GitLab

  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,866
    • Issues 4,866
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 455
    • Merge requests 455
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #18874
Closed
Open
Created Oct 22, 2020 by Julien Debon@Sir4ur0nDeveloper

Deriving multiple classes in standalone (or document this limitation)

Motivation

To the best of my knowledge (I could not find any information about it in the GHC Standalone Deriving documentation), it is not possible to derive multiple classes in a single standalone deriving declaration.

Repeating an identical deriving declaration only to change the derived instance seems like unnecessary boilerplate:

deriving instance Eq (MyType 'Foo)
deriving instance Show (MyType 'Foo)
deriving instance Read (MyType 'Foo)

But this situation can actually become bug-prone when used together with via strategy, as it becomes easy to have discrepancies. Example using deriving-aeson library:

deriving via
  ( CustomJSON
      '[ OmitNothingFields,
         UnwrapUnaryRecords,
         TagSingleConstructors,
         FieldLabelModifier (StripPrefix "user", CamelToSnake)
       ]
  )
  instance
    ToJSON (MyType 'Foo)

deriving via
  ( CustomJSON
      '[ OmitNothingFields, -- Oh, we forgot to add 'UnwrapUnaryRecords' here!
         TagSingleConstructors,
         FieldLabelModifier (StripPrefix "user", CamelToSnake)
       ]
  )
  instance
    FromJSON (MyType 'Foo)

Proposal

I propose GHC supports deriving several instances in a single standalone declaration. I don't have a strong opinion on the syntax (nor have I put a lot of effort in thinking about it, I have to admit), though I give an example of what it could look like below:

deriving instance (Eq, Show, Read) (MyType 'Foo)

deriving via
  ( CustomJSON
      '[ OmitNothingFields,
         UnwrapUnaryRecords,
         TagSingleConstructors,
         FieldLabelModifier (StripPrefix "user", CamelToSnake)
       ]
  )
  instance
    (ToJSON, FromJSON) (MyType 'Foo)

Should this proposal get rejected, I think we should at least document this limitation.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking