[Haskell-beginners] question about shared data types

Nikita Danilenko nda at informatik.uni-kiel.de
Mon Apr 29 13:47:10 CEST 2013


Hi, Ian,

you can use qualified imports to manage that. Then you can write

> module Test where
>
> import qualified Aaa as QualifiedA ( Foo )
>
> import Bbb                         ( Foo )

Below you can use barFoo by prepending QualifiedA e.g.

> fooA = QualifiedA.Foo "Hi, I'm from A"
> fooB = Foo "I belong to B"

More information is available at

http://www.haskell.org/haskellwiki/Import

Regards,

Nikita


On 29/04/13 12:28, Ian Knopke wrote:
> Hi everyone,
>
> I have two modules that share a common data type in their respective
> data structures:
>
> package Aaa where
>
>       newtype Foo = Foo String
>
>       data Bar = Bar {
>           barFoo :: [Foo]
>       } deriving (Show, Eq)
>       ...
>
> package Bbb where
>
>       newtype Foo = Foo String
>
>       data Quux = Quux {
>           quuxFoo :: [Foo]
>       } deriving (Show, Eq)
>       ...
>
> Both modules require the Foo newtype defined. However, in all other
> respects they are separate modules that can be used independently.
>
> Defining the newtype Foo twice, as above, throws a compilation error
> (ambigious reference). Importing one module inside another breaks
> independence. I've been including newtype Foo in a Common.hs module
> that is included in both, as I would in C or C++. This sort of thing
> can easily get more complicated as the program grows however.
>
> I was wondering if the Haskell community has a better solution to this
> that I've overlooked.
>
>
> Ian Knopke
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130429/cf02256a/attachment-0001.htm>


More information about the Beginners mailing list