Welcome to a straightforward and short blog post on how to export data in Haskell. When we speak of data, we mean types defined with the data keyword, and not type synonyms or any other related term. Typeclasses can also be exported in this way.
Exporting Data
For this example, let's define a simple data type, called Bit. Bit can hold either On or Off. The simple data declaration of bit is: data Bit = On | Off . In order to export this from our module, say, Binary.hs, we can either choose individual type constructors to export, or export them all by using the name of the Type followed by parenthesis and two dots.
To export just On: module Binary(Bit(On)) where
To export all of Bit's constructors: module Binary(Bit(On,Off)) where
To export all of Bit's constructors implicitly (The easy way, and what you will do 95% of the time): module Binary(Bit(..)) where
The (..) is basically saying "Export all of Bit's type constructors", which is good practice if you're using this type anywhere else.
Exporting Typeclasses
The same is true for Typeclasses. In exporting Typeclasses, you can choose individual Methods to export, or export them all with (..)
No comments:
Post a Comment