Module export
export syntax
Code example in this chapter:
module Shapes
where
import Data.Number (pi)
import Data.Ord (abs)
data Shape = Circle Number Number Number
| Rectangle Number Number Number Number
surface :: Shape -> Number
surface (Circle _ _ r) = pi * r * r
surface (Rectangle x1 y1 x2 y2) = (abs $ x2 - x1) * (abs $ y2 - y1)
export all (implicitly)
The following will export all:
module Shapes where
- types:
Shape
- value constructors:
Circle
andRectangle
- functions:
surface
Haskell reference
If the export list is omitted, all values, types and classes defined in the module are exported, but not those that are imported. https://www.haskell.org/onlinereport/modules.html
export all value constructors
The following code with export all value constructors of type Shape
: Circle
and Rectangle
:
module Shapes
(Shape (..)) where
not export value constructors
The following code will only export the data type Shape
, as a result the module which imports it, will not be able to construct value of Shape
.
module Shapes
(Shape) where
Not exporting the value constructors of a data types makes them more abstract in such a way that we hide their implementation. Also, whoever uses our module can't pattern match against the value constructors.
export function with its required data type
PureScript does not allow to export function without its required data type. Like the code below, if we don't export Shape, we will get compile error:
module Shapes
(surface, Shape) where
export partial constructors?
In PureScript (like Haskell), data constructors are all or nothing when it comes to exports. You can:
- Export just the type (hiding all constructors)
- Export the type and all its constructors
But you cannot selectively export only some constructors. The code below will not compile:
module Shapes
(Shape(Circle)) where
You cannot export only specific constructors of a data type in PureScript.