title: How do I make sure a function receives the same parameter type as the current object?
body: <pre><code>abstract class A {
    protected[this] type ThisType = this.type
    protected[this] type OtherType = this.type 
  
    def assign(other: OtherType): ThisType
  }
  
  class B extends A {
    def assign(other: OtherType): ThisType = ???
  }
  
  class C extends A {
    def assign(other: OtherType): ThisType = ???
  }
  
  class D extends C {
    def assign(other: OtherType): ThisType = ???
  }
  </code></pre>
  
  <p>How do I make sure the other type received in <code>assign</code> of and object of type <code>B</code> is also <code>B</code>. e.g. how can I write something effectively like:</p>
  
  <pre><code>def f1(p1: A, p2: A) = p1.assign(p2)
  def f2[T &lt;: A](p1: T, p2: T) = p1.assign(p2)
  </code></pre>
  
  <p>I am getting following errors:</p>
  
  <p><a href="https://i.stack.imgur.com/LTFQS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LTFQS.png" alt="Error at p1"></a>
  <a href="https://i.stack.imgur.com/rXVqZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rXVqZ.png" alt="Error at p2"></a></p>
  
  <p>NB: Actually <code>ThisType</code> and <code>OtherType</code> should be the same but I seperated them so I can try out different options.</p>