这是工厂想法的尝试(我以前做过这个并放弃了,但是让我们再试一次):
object Brickwall
case class Brickwall[A](brick: A)
trait Monoman {
var wall: Ref[this.type, Brickwall[String]]
def ref[V](v: V): Ref[this.type, V]
}
object Ref {
implicit def unwrap[Repr](r: Ref[_, Repr]): Repr = r.repr
implicit def wrap[A, Repr](repr: Repr): Ref[A, Repr] = new Impl[A, Repr](repr)
private class Impl[A, Repr](val repr: Repr) extends Ref[A, Repr]
}
trait Ref[A, Repr] { def repr: Repr }
def test(m: Monoman): Unit = {
val w0 = m.wall
val w1 = w0.copy(brick = "3.1415")
m.wall = w1 // doesn't convert to Ref
}
因此,虽然展开是透明的,但重新包装似乎不起作用,我怀疑不可能让它工作,因为m.type
永远无法推断。