Created
March 21, 2012 15:01
-
-
Save non/2148027 to your computer and use it in GitHub Desktop.
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
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