Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Driving a singleton type through a brickwall

Here is a very condensed version:

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

stupid brickwall doesn't let me through. any ideas how it might be possible? secret scala tunnel effects? hoping...

like image 228
0__ Avatar asked Dec 27 '22 22:12

0__


2 Answers

As far as I know the Scala compiler refuses to infer path dependent types, so a little type annotation helps:

def test( m: Monoman ) { m.me( Brickwall[m.type]( m ).otherSide )}
like image 82
Moritz Avatar answered Jan 11 '23 22:01

Moritz


Yes, singleton types are never inferred by the Scala compiler.

One possibility is to add a factory method to the Monoman trait:

trait Monoman { 
  def me( m: this.type ) : Unit
  def createWall = Brickwall[this.type](this) 
}

def test( m: Monoman ) { m.me(m.createWall.otherSide) }

Maybe that's not a viable solution in your case.

like image 27
Jesper Nordenberg Avatar answered Jan 11 '23 23:01

Jesper Nordenberg