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

On Sat, Jul 9, 2011 at 6:45 PM, Tom Murphy
On 7/9/11, yi huang
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.
I have to define it as a record, so aeson can inspect the attribute names and encode it to a json object automatically, for example: "{\"args\":[],\"id\":1,\"method\":\"test\",\"version\":1}" But normal data constructor would be encoded to an array. Though i can define it as a normal data constructor, and implement ToJSON class manually, but the code would be more verbose.
Tom

On Sat, Jul 9, 2011 at 5:59 PM, yi huang
On Sat, Jul 9, 2011 at 6:45 PM, Tom Murphy
wrote: On 7/9/11, yi huang
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. -- Jedaï

On Sun, Jul 10, 2011 at 12:25 AM, Chaddaï Fouché
On Sat, Jul 9, 2011 at 5:59 PM, yi huang
wrote: On Sat, Jul 9, 2011 at 6:45 PM, Tom Murphy
wrote: On 7/9/11, yi huang
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ï

On Sat, Jul 9, 2011 at 12:41, yi huang
On Sun, Jul 10, 2011 at 12:25 AM, Chaddaï Fouché
wrote:
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
If the first thing you do is "import Prelude ...", you shouldn't need the extension, IIRC? (Doesn't the Report spec that?)
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.
If they always have the same types (that is, "version" isn't an Int in one and String in another) then the -XDisambiguateRecordFields extension will help. -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
participants (3)
-
Brandon Allbery
-
Chaddaï Fouché
-
yi huang