Skip to content
CCPEDIAby Unity Nodes
App Development

Shortcut for declaring standalone deriving instances

4 posts198 views2 likesLast activity Sep 2022

  1. asarpeshkarOP

    It would be nice to have a more compact way to declare standalone deriving instances for polymorphic types.

    newtype Foo a = Foo a
    
    deriving instance Eq a => Eq (Foo a)
    
    deriving instance Show a => Show (Foo a)
    

    Is there such a shortcut? The declarations above are completely mechanical and get a bit tedious to read, especially when you’re defining multiple such types in close proximity.

  2. cocreature 1

    You don’t need to use standalone deriving at all here. The following does the trick:

    newtype Foo a = Foo a
      deriving (Show, Eq)
    
  3. asarpeshkar 1

    Weird, I could’ve sworn that the compiler complained in similar cases, but maybe I was thinking of examples where the instance type was different from context, e.g Eq a => Foo [a] or some such. Problem solved then, thanks.

  4. Ben_M

    Hi @asarpeshkar If @cocreature was able to resolve your query completely, you can mark his post as the Solution, this helps in housekeeping on the forum.