Created
December 22, 2016 03:43
-
-
Save fare/7620f8fd2708feedcec3bc5d8863eb9c to your computer and use it in GitHub Desktop.
Context passing in Scala
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 IsContext { | |
def contextualContent: List[HasContext[this.type]] = List() | |
contextualContent.map { item => item.setContext(this) } | |
} | |
trait HasContext[+Context] { | |
private var ctx: Any = null | |
def setContext(context: Any) { | |
assert(ctx == null) | |
ctx = context | |
} | |
lazy val context = ctx.asInstanceOf[Context] | |
} | |
trait Contextual[+Context] extends IsContext with HasContext[Context] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better, just pass along values in mutual recursive definitions: