Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Can i define a record without defining access    method. (yi huang)
   2. Re:  Can i define a record without defining       access method.
      (Tom Murphy)
   3. Re:  Can i define a record without defining       access method.
      (yi huang)
   4. Re:  Can i define a record without defining       access method.
      (Chadda? Fouch?)
   5.  iterating over a range of dates (Rolf Hanson)
   6. Re:  Can i define a record without defining       access method.
      (yi huang)


----------------------------------------------------------------------

Message: 1
Date: Sat, 9 Jul 2011 18:33:54 +0800
From: yi huang <yi.codepla...@gmail.com>
Subject: [Haskell-beginners] Can i define a record without defining
        access  method.
To: beginners@haskell.org
Message-ID:
        <cahu7ryzztgp_jruapvadl9cxai2+vym2thytosxpxlng_em...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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.


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

------------------------------

Message: 2
Date: Sat, 9 Jul 2011 06:45:15 -0400
From: Tom Murphy <amin...@gmail.com>
Subject: Re: [Haskell-beginners] Can i define a record without
        defining        access method.
To: yi huang <yi.codepla...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <cao9q0tvo_o9e8y53argy2x0qcyz3ziuvt15my7zkfsuyhpo...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On 7/9/11, yi huang <yi.codepla...@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



------------------------------

Message: 3
Date: Sat, 9 Jul 2011 23:59:51 +0800
From: yi huang <yi.codepla...@gmail.com>
Subject: Re: [Haskell-beginners] Can i define a record without
        defining        access method.
To: beginners@haskell.org
Message-ID:
        <CAHU7rYYTyJGjTHKEPBKW4uaGeZmYePrpo-AyZwpYtqh51B=3...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

 On Sat, Jul 9, 2011 at 6:45 PM, Tom Murphy <amin...@gmail.com> wrote:

> On 7/9/11, yi huang <yi.codepla...@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.
>

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
>



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

------------------------------

Message: 4
Date: Sat, 9 Jul 2011 18:25:13 +0200
From: Chadda? Fouch? <chaddai.fou...@gmail.com>
Subject: Re: [Haskell-beginners] Can i define a record without
        defining        access method.
To: yi huang <yi.codepla...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <canfjzrywoyhjqr79jloe_-u5vdofyt6uaxnl4_yyoq98ukw...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sat, Jul 9, 2011 at 5:59 PM, yi huang <yi.codepla...@gmail.com> wrote:
> ?On Sat, Jul 9, 2011 at 6:45 PM, Tom Murphy <amin...@gmail.com> wrote:
>>
>> On 7/9/11, yi huang <yi.codepla...@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.

-- 
Jeda?



------------------------------

Message: 5
Date: Sat, 9 Jul 2011 11:33:42 -0500
From: Rolf Hanson <rolf.han...@gmail.com>
Subject: [Haskell-beginners] iterating over a range of dates
To: beginners@haskell.org
Message-ID: <84375453-0be3-4cee-ad35-1db749f40...@gmail.com>
Content-Type: text/plain; charset=us-ascii

Hi, I'm trying to iterate over a range of dates and print them out.
Here's how I am doing it in Ruby:

'require 'date'

now      = Date.parse('2011-07-11')
end_date = Date.parse('2011-12-31')

(now..end_date).each do |d|
  # print out a date that looks like:
  # Monday, July 11, 2011
  puts "#{d.strftime('%A, %B %d, %Y')}"
end

I've been looking at the docs for Data.Time.Format and Data.Time.Calendar and 
am a bit puzzled at where to begin. Anyone have ideas? I've not found many 
(any?) examples of time and date manipulation in Haskell. 

RW


------------------------------

Message: 6
Date: Sun, 10 Jul 2011 00:41:07 +0800
From: yi huang <yi.codepla...@gmail.com>
Subject: Re: [Haskell-beginners] Can i define a record without
        defining        access method.
To: Chadda? Fouch? <chaddai.fou...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <cahu7rybkw2vgpyc6pfoszh5wnos-xfqquy18uqj_y+uynjd...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Sun, Jul 10, 2011 at 12:25 AM, Chadda? Fouch?
<chaddai.fou...@gmail.com>wrote:

> On Sat, Jul 9, 2011 at 5:59 PM, yi huang <yi.codepla...@gmail.com> wrote:
> >  On Sat, Jul 9, 2011 at 6:45 PM, Tom Murphy <amin...@gmail.com> wrote:
> >>
> >> On 7/9/11, yi huang <yi.codepla...@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.htm>

------------------------------

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 37, Issue 15
*****************************************

Reply via email to