Skip to content

Instantly share code, notes, and snippets.

@vito
Created September 12, 2010 14:46
Show Gist options
  • Select an option

  • Save vito/576158 to your computer and use it in GitHub Desktop.

Select an option

Save vito/576158 to your computer and use it in GitHub Desktop.
parameters
Parameter = Object clone
Parameter new: v := Parameter clone do: {
value: (self) = v
value: _ = v
}
(p: Parameter) _? := p value: self
(p: Parameter) set: v :=
(p) value: (self) = v
with: (p: Parameter) as: new do: (action: Block) := {
old = p _?
p set: new
p ensuring: @(set: old) do: action
} call
@vito
Copy link
Copy Markdown
Author

vito commented Sep 12, 2010

Example:

x = Parameter new: 0
foo := bar
bar := x _? print
foo
  => 0
with: x as: 1 do: { foo }
  => 1

And it's thread-safe!

x = Parameter new: 0
{ Timer sleep: 5 seconds; x _? print } spawn
x set: 42
x _? print
  => 42 is the value here
  => 0 is printed by the other process after 5 seconds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment