How to use instanceof with scala object?

Viewed 1739

I've the following scala hierarchy:

sealed trait SessionResult[+T] {
  def toOption: Option[T]
}

object SessionResult {
  trait SessionValue[T] extends SessionResult[T] {
    def session: T
    def toOption: Option[T] = Some(session)
  }
  trait NoSessionValue[T] extends SessionResult[T] {
    def toOption: Option[T] = None
  }

  case class Decoded[T](session: T) extends SessionResult[T] with SessionValue[T]
  case class CreatedFromToken[T](session: T) extends SessionResult[T] with SessionValue[T]

  case object NoSession extends SessionResult[Nothing] with NoSessionValue[Nothing]
  case object TokenNotFound extends SessionResult[Nothing] with NoSessionValue[Nothing]
  case object Expired extends SessionResult[Nothing] with NoSessionValue[Nothing]
  case class Corrupt(e: Exception) extends SessionResult[Nothing] with NoSessionValue[Nothing]
}

But I use this code from java and the following piece of code does not compile:

SessionResult<SomeSession> sr = ...
System.out.println(sr instanceof NoSession)

Why? And also how can I use instanceof to check the class of scala's object?

The error I'm getting is:

Inconvertible types; cannot cast SessionResult<SomeSession> to NoSession.
1 Answers
Related