[Haskell-beginners] Can i define a record without defining access method.

Tom Murphy amindfv at gmail.com
Sat Jul 9 12:45:15 CEST 2011


On 7/9/11, yi huang <yi.codeplayer at gmail.com> wrote:
> I'm trying to create a haskell implementation of json rpc, I try to define
> protocol using record like this:
>
> data Request = Request {
>     version :: Integer
>   , id      :: Integer
>   , method  :: String
>   , args    :: [Value]
> } deriving (Typeable, Data, Show)
>
> data Response = Response {
>     version :: Integer
>   , id      :: Integer
>   , code    :: Integer
>   , method   :: String
>   , result  :: Value
> } deriving (Typeable, Data, Show)
>
> so i can use json library to encode/decode it.
> But this code fails, because haskell will define access function
> automaticlly, and function names conflicts.
> My question is, is there a way i can define record without access function,
> so i can have same attribute name in multiple record.


If you don't want access functions defined, you can simply not name
your record fields:

data Request = Request Integer Integer String [Value]
   deriving (Typeable, Data, Show)


If you want it to be more readable, you can define type synonyms:

type ID = Integer
type Version = Integer
[...]

data Request = Request Version ID Method Args
   deriving (Typeable, Data, Show)

The two "instances" of ID won't conflict, then.


Tom



More information about the Beginners mailing list