Skip to content
CCPEDIAby Unity Nodes
Discussions/App Development/Shortcut for declaring standalone deriving instancesForum ↗

Shortcut for declaring standalone deriving instances

App Development4 posts177 views2 likesLast activity Sep 2022
AS
asarpeshkarOP
Sep 2022

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.

CO
cocreature
Sep 2022 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)
AS
asarpeshkar
Sep 2022 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.

BE
Ben_M
Sep 2022

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.

← Back to Discussions