Is there something like Show (deriving Show) that only uses an algebraic datatype's constructors? (please don't mind that I'm using the word constructor, I don't know the right name...)
The reason for this question is that with many of my algebraic datatypes I don't want to bother with making their contents also derive Show, but I still want to gain some debug information about the constructor used without having to implement showing every constructor...
An alternative could be a function that gives me the constructors name, that I can use in my own implementation of show.
This of course needs to do some compiler magic (auto deriving) because the whole idea behind is to not have to explicitely implement every data constructors string representation.
A more explicit approach is to create a custom derivation via TemplateHaskell. The following code describes the logic for generating custom
Showinstances for a given datatype:Then, you simply do
...and you can happily
showit. Note that both code snippets must be placed in separate modules and you need to useTemplateHaskellextension.I took a lot of inspiration from this article.