Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of showsPrec over show?

Tags:

haskell

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?

like image 267
Xodarap Avatar asked Oct 14 '18 15:10

Xodarap


1 Answers

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

like image 177
Dan Burton Avatar answered Oct 12 '22 22:10

Dan Burton