scalacompiler-errorsself-type

Self type annotation not recognized by compiler


I want a class to force it's subclasses to implement a sub-trait to a trait and tried this:

sealed trait TA
sealed trait TB extends TA
sealed trait TC extends TA

sealed trait CA {
  this: TA =>
}
final class CB extends CA with TB
final class CC extends CA with TC

def ca: CA = if (scala.util.Random.nextBoolean) new CB()
             else new CC()
def ta: TA = ca

With the code below I get the following compiler error:

Error:(16, 16) type mismatch;
 found   : CA
 required: TA
  def ta: TA = ca
  1. Could a CA not be a TA when I have the self type annotation: "this: TA =>" or is this a glitch in the compiler?
  2. Are there better ways to implement this?

Solution

  • Self type is a kind of "private inheritance" (well, it's not really inheritance, but similar), you can't use it outside of the class.

    You could have your CA implement the conversion if you have to:

    trait CA { this: TA => 
      def ta: TA = this
    }
    

    As to your question about a "better way to implement this", it's hard to tell, because it's not clear at all what it is you are trying to do here.

    Why not have CA extend TA for example? Like I said, self-type is very similar to a "private inheritance", and you sound like you want inheritance, but don't want it to be private. So ... why not just use inheritance?