通过砖墙驱动单件类型

这是一个非常简洁的版本:
case class Brickwall[A](otherSide: A)
trait Monoman { def me(m: this.type): Unit }

def test(m: Monoman): Unit = m.me(Brickwall(m).otherSide)

-> error: type mismatch;
 found   : Monoman
 required: m.type
愚蠢的砖墙不让我通过。任何想法如何可能?秘密斯卡拉隧道的影响?希望...     
已邀请:
据我所知,Scala编译器拒绝推断路径依赖类型,因此一点类型注释有助于:
def test( m: Monoman ) { m.me( Brickwall[m.type]( m ).otherSide )}
    
是的,Scala编译器永远不会推断单例类型。 一种可能性是将工厂方法添加到Monoman特征:
trait Monoman { 
  def me( m: this.type ) : Unit
  def createWall = Brickwall[this.type](this) 
}

def test( m: Monoman ) { m.me(m.createWall.otherSide) }
在你的情况下,这可能不是一个可行的解决方案。     
这是一个尝试与工厂的想法(我以前做过这个并放弃了,但我们再试一次):
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
永远不会被推断。     

要回复问题请先登录注册