Skip to content

Instantly share code, notes, and snippets.

@non
Created March 21, 2012 15:01
Show Gist options
  • Save non/2148027 to your computer and use it in GitHub Desktop.
Save non/2148027 to your computer and use it in GitHub Desktop.
trait WithContext {
var ctx = null
def setContext(c:Context): Unit = if (ctx == null) {
ctx = c
} else {
sys.error("already set")
}
def fromContext[T](k:String):T = if (ctx == null) {
sys.error("premature access")
} else {
ctx.get(k).asInstanceOf[T]
}
}
class MyBundle extends WithContext {
lazy val foo = fromContext[Int]("foo")
lazy val bar = fromContext[Widget]("bar")
lazy val qux = fromContext[Double]("qux")
def onCreate(c:Context) {
setContext(c)
...
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment