Reputation: 4502
Suppose I have an abstract class and an object of the same name in the same file.
How can I explicitly tell the compiler that the return type of run
is the abstract class CommandResult type not the object CommandResult type?
Base.scala
trait Base {
def run[T]: Future[T]
}
X.scala
class X extends Base {
override def run[CommandResult]: Future[CommandResult] = {
// here return CommandResult, Success or Failure
}
}
sealed abstract class CommandResult(val exitCode: Int)
object CommandResult {
case object Success extends CommandResult(0)
case object Failure extends CommandResult(1)
}
Upvotes: 0
Views: 55
Reputation: 12783
When you write def run[CommandResult]
, it means "run takes a type one parameter named CommandResult", that's why you are getting error like:
On line 3: error: type mismatch;
found : CommandResult.Success.type
required: CommandResult <--- The type parameter,
not the class or object.
The error indeed is very misleading. For example try override def run[X]: Future[X]
and will see "required: X`.
If you do as Luis suggests:
trait Base[T] {
def run: Future[T]
}
class X extends Base[CommandResult] {
override def run: Future[CommandResult] = {
Future(CommandResult.Success)
}
}
It will work.
Upvotes: 1