An export modifier controls what importing modules can access: a name, its type, its definition, or a data type’s constructors. section 10.3.1
Choose the modifier according to the interface importers need:
- private hides a declaration from other modules. Declarations without a modifier are private by default. section 10.3.1
- export exposes a declaration’s name and type, but not its definition; for data types, constructors remain hidden. section 10.3.1
- public export exposes definitions and data constructors as well as names and types. Use it when other modules need a function’s behavior in a type, such as with type-level functions or type synonyms. section 10.3.1 section 15.2.5
- Prefer export over exposing a function’s definition unless another module needs that definition. section 15.2.5
Keep a data type abstract
Export Shape but not its constructors, then export functions such as triangle and rectangle to create values. Importing modules can use those functions but cannot pattern-match on Shape. section 10.3.1
module Shape_abs export #1 data Shape = Triangle Double Double | Rectangle Double Double | Circle Double export triangle : Double -> Double -> Shape #2 triangle = Triangle export rectangle : Double -> Double -> Shape #2 rectangle = Rectangle export circle : Double -> Shape #2 circle = Circle
The module listing shows Shape’s constructors kept behind exported creation functions. section 10.3.1
