Skip to content

Instantly share code, notes, and snippets.

@micseydel
Created August 4, 2018 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micseydel/4815f18af52b99606fb99ef8e22ecc31 to your computer and use it in GitHub Desktop.
Save micseydel/4815f18af52b99606fb99ef8e22ecc31 to your computer and use it in GitHub Desktop.
import scala.collection.mutable
object Example {
case class Parent(private val childLocator: ChildLocator) {
lazy val child = childLocator.locate(from = this)
}
case class Child(parent: Parent)
class ChildLocator { // mutable
private val mapping = mutable.HashMap[Parent, Child]()
def locate(from: Parent) = mapping(from)
def insert(from: Parent, to: Child): Unit = {
mapping(from) = to
}
}
def main(args: Array[String]): Unit = {
val childLocator = new ChildLocator()
val parent = Parent(childLocator)
val child = Child(parent)
childLocator.insert(parent, child)
println(parent.child)
println(child.parent)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment