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

yi huang yi.codeplayer at gmail.com
Sat Jul 9 18:41:07 CEST 2011


On Sun, Jul 10, 2011 at 12:25 AM, Chaddaï Fouché
<chaddai.fouche at gmail.com>wrote:

> On Sat, Jul 9, 2011 at 5:59 PM, yi huang <yi.codeplayer at gmail.com> wrote:
> >  On Sat, Jul 9, 2011 at 6:45 PM, Tom Murphy <amindfv at gmail.com> wrote:
> >>
> >> 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)
> >>
> >> > 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 really want to keep those attribute names exactly, you'll have
> to check where the conflict is and find a way not to import the
> conflicting function name. Here I guess "id" is the big problem since
> it comes with the prelude you'll have to use the NoImplicitPrelude
> extension and explicitly "import Prelude hiding (id)", I suggest you
> make a separate module for your type definition so you don't have to
> worry about that in the rest of your code.
>

Sorry i don't describe my problem well, actually i have two records, Request
and Response, some attributes have same names, e.g. version, id.

data Request = Request {
    version :: Int
    ...
}
data Response = Response {
    version :: Int
    ...
}

And yes, i really want to keep those names exactly, so aeson can
automatically encode them to right json object.
If there are no way to hide access function, then i guess i have to define
them in seperate module, or define them as normal data constructor with
encode/decode precedure defined manually. Both is not pleasant to me.


> --
> Jedaï
>



-- 
http://www.yi-programmer.com/blog/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110710/63cc080c/attachment-0001.htm>


More information about the Beginners mailing list