Pretty-printing in ghci
Asked Answered
O

1

12

Is there a way to make ghci use a custom pretty-printing function instead of show for certain types? A more general question: what are the general guidelines to make a library as usable as possible in interactive mode? Thanks.

Outland answered 12/7, 2016 at 21:29 Comment(0)
C
12

You can specify a custom pretty-printing function using the --interactive-print flag and naming any function in scope with the type C a => a -> IO () for any constraint C. (See Section 2.4.9 of the docs for details.)

ghci --interactive-print=MyModule.prettyPrint

This means you can specify your own function from your own typeclass. There's no way to do this just for a specific type, but your custom class can always include a fallback instance like

instance Show a => PrettyPrint a where prettyPrint = show

This will require at least OverlappingInstances to work.

Cristobal answered 12/7, 2016 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.