55 lines
1.6 KiB
Plaintext
55 lines
1.6 KiB
Plaintext
Module CheckSingleton {
|
|
\\ singleton
|
|
\\ pointers and static groups are the same object because
|
|
\\ each one has a pointer to same state (a tuple)
|
|
\\ but from outside we do the miracle to have a static group to act as a pointer
|
|
\\ We need a lambda function to hold the pointer to Singleton as closure
|
|
Global One=lambda M=pointer() (aValue=0)-> {
|
|
If M is type null then
|
|
\\ one time happen
|
|
Group Singleton {
|
|
Type:One
|
|
Private:
|
|
state=(aValue,)
|
|
Public:
|
|
module Add (x) {
|
|
.state+=x
|
|
}
|
|
Set {Drop}
|
|
Value {
|
|
=.state#val(0)
|
|
}
|
|
}
|
|
M->group(Singleton)
|
|
end if
|
|
\\ return M which is a pointer
|
|
=M
|
|
}
|
|
K=One(100)
|
|
Print Eval(K)=100
|
|
M=One()
|
|
Print Eval(M)=100
|
|
Print K is M = true
|
|
Print K is type One = true
|
|
K=>add 500
|
|
Print eval(K)=600
|
|
\\ copy K to Z (no pointer to Z, Z is named group)
|
|
Z=Group(K)
|
|
Print eval(z)=600, z=600
|
|
Z.add 1000
|
|
Print Z=1600, Eval(M)=1600, Eval(K)=1600
|
|
\\ push a copy of Z, but state is pointer so we get a copy of a pointer
|
|
Push Group(Z)
|
|
Read beta
|
|
Beta.add 1000
|
|
Print Z=2600, Eval(M)=2600, Eval(K)=2600
|
|
\\ convert pointer to group (a copy of group)
|
|
group delta=One()
|
|
delta.add 1000
|
|
Print Z=3600, beta=3600, delta=3600, Eval(M)=3600, Eval(K)=3600
|
|
\\ M and K are pointers to groups
|
|
M=>add 400
|
|
Print Z=4000, beta=4000, delta=4000, Eval(M)=4000, Eval(K)=4000
|
|
}
|
|
CheckSingleton
|