Created
August 4, 2018 05:05
-
-
Save micseydel/4815f18af52b99606fb99ef8e22ecc31 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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