Search code examples
purescript

New Show instance for existing type


I have wrapper type for NonEmptyList of my custom type Cell:

instance Show Cell where
  show = ...

type CellsRow = NonEmptyList Cell

I trying to create Show instance for Cells as well with:

instance Show CellsRow where
  show = foldMap show

But it raises Orphan instance found for .... I can resolve it by making CellsRow newtype, but can I use both Show instance and simple type CellsRow = NonEmptyList Cell?


Solution

  • No, you cannot declare multiple instances for the same type. This is on purpose, otherwise there would be ambiguity in instance resolution.

    The error refers to a subset of this - what is called "orphan" instances. These are such instances that neither the class definition nor the type definition are in the same module. In PureScript you can only declare instances either in the same module as the type or in the same module as the class.