In the previous post, "Closure" as a variant of the Mathematica OOP was presented. Next, i introduce a closure which method is written by the pure function. This sample shows also a initialization method for the OOP instance.
First code is a class definition,
class[nam_[init_]] := Module[{local = init},
nam & ^:= ++local;
]
Second code is a construction of instances with local value initialization as,
class[counter1[0]]; class[counter2[100]];
Then we can call the initialized instance as a pure function,
{counter1 &, counter2 &, counter1 &, counter2 &}
We can get the results as,
{1, 101, 2, 102}
Let's examine the "counter1"
?counter1
We'll get as,
Global `counter1
counter1&^:=++local$231
Counter1 was encapsulated by OOP, and is still a global symbol, we can do as,
{counter1 = 0, counter1 &, counter1, counter1 &}
The result is,
{0, 3, 0, 4}