Singleton: Difference between revisions
m
→{{header|Oforth}}
(→{{header|Go}}: expanded with sync.Once examples) |
|||
Line 1,413:
Oforth does not have global variables, class attributes or some kind of shared mutable memory that can be updated by different tasks.
In Oforth, singleton is an anti-pattern because it needs synchronisation in order to be safe between parallel tasks.
If the goal is to keep and update a value in a safe way, a channel can be used.
Line 1,421:
<lang Oforth>Object Class new: Sequence(channel)
Sequence method: initialize(initialValue)
Channel newSize(1) := channel
@channel send(initialValue) drop ;
Sequence method: nextValue
Usage :
<lang Oforth>
: testSequence
| s i |
Sequence new(0) ->s
100 loop: i [ #[ s nextValue println ] & ] ;</lang>
=={{header|ooRexx}}==
|