Stateful Modules
Using a mechanism called tuple modules, we can arrange to encapsulate state together with a module name. We can use this mechanism for information hiding and creating adapter modules that hide the details of an interface from programs that use the interface. This is useful if we want to make a common interface to several dissimilar modules or mimic some of the features of object-oriented programming.
When we call X:Func(....), X does not have
to be an atom. It can be a tuple. If we write X = {Mod, P1, P2,
..., Pn} and then call X:Func(A1, A2, ..., An),
then what actually gets called is Mod:Func(A1, A2, ..., An,
X).
For example, the call {foo,1,2,3}:bar(a,b)
gets converted to the call foo:bar(a,b,{foo,1,2,3}).
Using this ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access