Created
November 24, 2012 12:03
-
-
Save edofic/4139372 to your computer and use it in GitHub Desktop.
Scala atom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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