How to organize code

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to organize code

L.Guo
Hi,

How do you organize code ?

Here is a sample.
Acturally, I am thinking about using this plan.

Any suggestions ?

> -- BasicalType.hs
> type Position = (Int,Int)
> data Box = Box { pos :: Position }
> data Chain = Chain { pos :: [Position] }

> -- Object.hs
> import BasicalType
> class Object o where
>   pos :: o -> [Position]

> -- Type.hs
> import BasicalType
> import Object
> instance Object Box where
>   pos = return . BasicalType.pos
> instance Object Chain where
>   pos = BasicalType.pos

> -- Main.hs
> import Type
> ...

    Regards
--------------
L.Guo
2008-01-28

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to organize code

Tim Chevalier
On 1/27/08, L.Guo <[hidden email]> wrote:

> Hi,
>
> How do you organize code ?
>
> Here is a sample.
> Acturally, I am thinking about using this plan.
>
> Any suggestions ?
>
> > -- BasicalType.hs
> > type Position = (Int,Int)
> > data Box = Box { pos :: Position }
> > data Chain = Chain { pos :: [Position] }
>
> > -- Object.hs
> > import BasicalType
> > class Object o where
> >   pos :: o -> [Position]
>
> > -- Type.hs
> > import BasicalType
> > import Object
> > instance Object Box where
> >   pos = return . BasicalType.pos
> > instance Object Chain where
> >   pos = BasicalType.pos
>
> > -- Main.hs
> > import Type
> > ...
>

Be careful not to confuse Haskell's type class system with the idea of
a "class" in object-oriented programming. I suggest you read this
article:
http://trevion.blogspot.com/2007/02/snowball-chance.html

Cheers,
Tim

--
Tim Chevalier * http://cs.pdx.edu/~tjc * Often in error, never in doubt
"...It's a thin line between amateur and professional, a line mostly
emphasized by insecure professionals." -- Bob Franke
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to organize code

Bugzilla from jonathanccast@fastmail.fm
In reply to this post by L.Guo
On 27 Jan 2008, at 11:18 PM, L.Guo wrote:

> Hi,
>
> How do you organize code ?
>
> Here is a sample.
> Acturally, I am thinking about using this plan.
>
> Any suggestions ?
>
>> -- BasicalType.hs
>> type Position = (Int,Int)
>> data Box = Box { pos :: Position }
>> data Chain = Chain { pos :: [Position] }
>
>> -- Object.hs
>> import BasicalType
>> class Object o where
>>   pos :: o -> [Position]
>
>> -- Type.hs
>> import BasicalType
>> import Object
>> instance Object Box where
>>   pos = return . BasicalType.pos
>> instance Object Chain where
>>   pos = BasicalType.pos
>
>> -- Main.hs
>> import Type
>> ...

I would recommend against this; Type.hs should define the types  
directly, and define or import the Object class.

In particular, IIRC GHC wants ever instance in the same module as  
either the type or the class definition, for performance reasons  
(this is the most common pattern, so it gets special optimizations).

jcc

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe