I’m using a Haskell library for OAuth and the author didn’t derive Show for a type I am using and would like to be able to print out for debugging. I would like to derive Show for it. Is there any way to do this from outside the library, apart from building up a function copies all the record fields into a record type that does derive Show?
The type in question is Token from Network.OAuth.Consumer:
You can use a GHC extension called
StandaloneDeriving.With this extension, you can write expressions like:
To use this, put
at the top of your file.
The syntax for a standalone derivation is essentially exactly the same as the syntax for an
instancestatement, except preceded byderivingand without awhereclause. This means you can write more specific instance like:You also have to explicitly give the context needed for your instance. You would have to write a standalone instance like this:
That is, you have to explicitly note the
Show acontext needed.This shouldn’t come up in your case, but it’s something to keep in mind.