Accessing
We can access the components of a namespace by means of the selection
operator ".
", as with records.
Let us suppose that we keep an amount in euros using the previously
defined namespace Euro
. Then, we can write a tip
function outside
Euro
that increments a given amount each time it is called.
In principle, we could change the implementation of Euro
, without
having to change the storage
type or the function tip
. For
example, if we decide later that we should support manipulating
negative values, we could change Euro
as follows:
The code of tip
still works, and no change is needed. Abstraction
accomplished!
Note that code using the namespace Euro
might still break the
abstraction if it directly uses the underlying representation of
Euro.t
. Client code should always try to respect the interface
provided by the namespace, and not make assumptions on its current
underlying representation. For example, Euro.t
is a transparent
alias of nat
(or int
). In order to hide the representation of a
type in a namespace, we need to constrain the namesapce with an
interface.