Skip to content

Instantly share code, notes, and snippets.

@ymasory
Forked from non/gist:2148027
Created March 21, 2012 15:20
Show Gist options
  • Save ymasory/2148335 to your computer and use it in GitHub Desktop.
Save ymasory/2148335 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