Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide fields from deriving (Show)

Tags:

haskell

record

Imagine I have a data record with many fields:

data DataRecord = DataRecord {
    field1 :: String,
    field2 :: String,
    ...
} deriving (Show)

Is it possible to hide some fields from the deriving (Show) or do have to implement my own show function for DataRecord?

Reason for my question: When I have cyclic dependencies between two data records both using deriving (Show) the show function would generate an infinite string.

like image 623
Stephan Kulla Avatar asked Sep 18 '25 09:09

Stephan Kulla


1 Answers

The Haskell 2010 report mentions your cyclic dependencies as unsuitable case:

The derived Read and Show instances may be unsuitable for some uses. Some problems include:

  • Circular structures cannot be printed or read by these instances.

So you need to specify the instance by hand.

like image 191
Zeta Avatar answered Sep 20 '25 02:09

Zeta