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 ...

Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.