0

Imagine that we are defining a custom Display typeclass that acts like Show but with a prettier formatting:

class Display a where
    display :: a -> PrettyFormat

Would it be possible to make a simple function prettyPrint that calls display if the Display type class is instantiated for the data type, and show if it's not?

Nick Tchayka
  • 563
  • 3
  • 14
  • This can only work if the compiler compiles the whole program and libraries in a single gulp, or forbidding orphan instances. Incremental compilation pretty much copes with that, since the compiler does not know if another module will define a `Display` instance. – chi Feb 14 '17 at 20:09
  • 3
    I reckon the best way to convince you _not_ to try this is to show you [what it involves](https://wiki.haskell.org/GHC/AdvancedOverlap). And even that solution suffers from a couple really unpleasant problems. – Alec Feb 14 '17 at 20:25
  • @Alec any idea so it doesn't involve instantiating my typeclass for each Haskell type? – Nick Tchayka Feb 14 '17 at 21:54
  • 1
    @NikitaTchayka The best approach (which I would've suggested if this question had not been closed) is to make a `default` signature for `Display` with a `Show` constraint. Then, if you automatically derive `Display` you end up with the `Show` variant, but you can still customize the output with hand written `Display` variants if you want. – Alec Feb 14 '17 at 21:59

0 Answers0