Picture and types of cake

How def someA(c trait B) to use trait Awith the same C#MyTypeas in B? (Then A#MyType =:= B#MyType)

trait C {
  type MyType
}


trait A {
  self: C =>
  def doSomething(s: MyType) { println(s.toString)}
}

trait B {
  self: C =>  

  def someA: A
  def myType: MyType

  def action = someA.doSomething(myType)
}

// Mix part

case class Ahoy(value: String)

trait ConcreteC extends C {  
  type MyType = Ahoy
}


class PieceOfCake extends B with ConcreteC {
  val someA = new A with ConcreteC
  val myType = Ahoy("MyType")

}

Not compiled: type mismatch;

[error]  found   : B.this.MyType
[error]  required: _1.MyType where val _1: A
[error]   def action = someA.doSomething(myType))
+5
source share
2 answers

You can declare doSomethingand myTypeuse regardless of the route version myType, SomeType#MyType:

trait SomeType {
  type MyType
}


trait A {
  self: SomeType =>
  def doSomething(s: SomeType#MyType) { println(s.toString)}
}

trait B {
  self: SomeType =>  

  def someA: A
  def myType: SomeType#MyType

  def action = someA.doSomething(myType)
}
+3
source

I'm sure you cannot do this, since path-independent types are just that if A <B, then A # T is strictly different from B # T (i.e. A # T will never = : = B # T).

As they say, it is safe to throw, so you can always do something like someA.doSomething(myType.asInstanceOf[someA#MyType]). Ugly but working.

0
source

All Articles