Revert "Revert "Made use of ::= in core libraries and defined a RebindError condition...
[cslatevm.git] / src / lib / suspension.slate
blob1858805925f82e2d4a34d9751279974f5267dce1
1 prototypes define: #Suspension &parents: {Cloneable} &slots:
2  {#closure -> []. #value -> Nil}.
3 "A suspension is a simple object that holds a closure for future evaluation.
4 The force method returns its ultimate value transparently and immediately.
5 This differs from the more complex Promise setup for eventual-sends."
7 b@(Method traits) suspend
8 "Create a new suspension to evaluate the Method."
9 [Suspension clone `setting: #{#closure. #value} to: {b. Nil}].
11 s@(Suspension traits) hasEvaluated
12 "The closure is wiped away once evaluation is successful, so we test that."
13 [s closure isNil].
15 s@(Suspension traits) force
16 "Depending on whether the Suspension has been evaluated, return the stored
17 result or evaluate the closure and update the state before returning that
18 result."
20   s hasEvaluated
21     ifTrue: [s value]
22     ifFalse:
23       [| result |
24        result := s closure do.
25        s closure := Nil.
26        s value := result]