A job candidate submitted a code sample to me which implements Show
by using showsPrec
instead of show
. This seems like an unusual enough choice that he must've had a reason to do this, but I can't find anything online about the advantages of this.
Is there any reason to use showsPrec
?
The showsPrec
function receives an additional argument: "the operator precedence of the enclosing context".
The main use case of defining showsPrec
instead of show
is so that you can decide whether to show your value with parens around it or not. When defining showsPrec
, it is common to use showParen
.
See, for example, the source for instance Show KindRep
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With