I think this solution is the simplest:
class Alpha(_beta: => Beta) {
lazy val beta = _beta
}
class Beta(_alpha: => Alpha) {
lazy val alpha = _alpha
}
// Exiting paste mode, now interpreting.
defined class Alpha
defined class Beta
scala> lazy val (alpha: Alpha, beta: Beta) = (new Alpha(beta), new Beta(alpha))
alpha: Alpha = <lazy>
beta: Beta = <lazy>
scala> alpha.beta
res2: Beta = Beta@4a40050
scala> beta.alpha
res3: Alpha = Alpha@38f18cc3
You could also add a factory for an alpha beta pair:
object AlphaBeta {
def apply() = {
lazy val tuple @ (alpha: Alpha, beta: Beta) = (new Alpha(beta), new Beta(alpha))
tuple
}
}
scala> AlphaBeta()
res13: (Alpha, Beta) = (Alpha@2a3fa87a,Beta@394df741)