Skip to content

Instantly share code, notes, and snippets.

@edofic
Created November 24, 2012 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edofic/4139372 to your computer and use it in GitHub Desktop.
Save edofic/4139372 to your computer and use it in GitHub Desktop.
Scala atom
/*
atoms
------------------------------------
non local lazy val
"write once var"
externally pure, interanally mutable
remote attributes
*/
class Atom[A] {
private var value: A = _
private var isSet = false
private var isForced = false
protected def populate(){
sys.error("cannont self-populate atom")
}
def update(a: A) {
if (!isSet || !isForced){
value = a
isSet = true
}
}
def apply(): A {
isForced = true
if (isSet) {
value
} else {
populate()
if (!isSet) {
sys.error("value not set")
}
value
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment