How to hide name and age in data People a = Person { name::a , age::Int} deriving Show in haskell

haskell

Solution

You can always provide a custom `Show` instance for all your types if you want:

data People a = Person { name::a , age::Int} 

instance (Show a) => Show (People a) where
    show (Person name age) == "Person " ++ show name ++ " " ++ show age

Or alternatively and less elegantly, write custom accessors:

data People a = Person a Int deriving Show
name (Person n _) = n
age (Person _ a) = a

Either way, you have to change the declaration of `People`, otherwise you are stuck with the derived `Show` instance.

As a side note, if you have a data type with only a single constructor, you typically name the constructor after the type, so it would be `data Person a = Person { name :: a, age :: Int }`

Problem

Suppose we have ``` data People a = Person { name::a , age::Int} deriving Show ``` in Hugs when I type ``` > Person "Alihuseyn" 20 ``` I get `Person {name = "Alihuseyn", age = 20}` but I want to get `Person Person "Alihuseyn" 20`. I mean how can I hide the mention of name and age without changing data?

Original source