3

I am trying to execute a akka-http which is a scala program. My KISS code is as follows:-

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpRequest, HttpResponse}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.directives.BasicDirectives
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Flow
import com.typesafe.config.ConfigFactory


object MyBoot02 extends SprayCanBoot02 with RestInterface02 with App {

}

abstract class SprayCanBoot02 {


  val config = ConfigFactory.load()
  val host = config.getString("http.host")
  val port = config.getInt("http.port")


  implicit val system = ActorSystem("My-ActorSystem")
  implicit val executionContext = system.dispatcher
  implicit val materializer = ActorMaterializer()
  //implicit val timeout = Timeout(10 seconds)

  implicit val routes: Flow[HttpRequest, HttpResponse, Any]

  Http().bindAndHandle(routes, host, port) map {
    binding => println(s"The binding local address is ${binding.localAddress}")
  }
}

trait RestInterface02 extends AncileDemoGateway02 with Resource02 {

  implicit val routes = questionroutes
  val buildMetadataConfig = "this is a build metadata route"
}

trait Resource02 extends QuestionResource02

trait QuestionResource02 {
  val questionroutes = {
    path("hi") {
      get {
        complete("questionairre created")
      }
    }
  }
}

class AncileDemoGateway02 {
  println("Whatever")
}

The error that I get is because of how I am wiring stuff when trying to execute MyBoot02. The error is as follows:

Error:(58, 41) illegal inheritance; superclass SprayCanBoot is not a subclass of the superclass AncileDemoGateway of the mixin trait RestInterface object MyBoot extends SprayCanBoot with RestInterface with App

Why does the error state 'SprayCanBoot is not a subclass of the superclass AncileDemoGateway'. In my code SprayCanBoot and AncileDemoGateway are 2 separate entities then why such an error?

Thanks

sdinesh94
  • 1,138
  • 15
  • 32

2 Answers2

4

In general, a trait inheriting from a class is commonly used to limit the classes a trait can be mixed into.

In your case, the MyBoot02 class can’t extend the RestInterface02 trait, because MyBoot02 and RestInterface02 don’t share the same superclass.

Sebastian
  • 16,813
  • 4
  • 49
  • 56
  • could you provide a reference to your answer. I don't think that is the issue here. Thanks – sdinesh94 May 08 '17 at 22:30
  • The superclass of `MyBoot02` is `SprayCanBoot02`, the superclass of `RestInterface02` is `AncileDemoGateway02`. As `SprayCanBoot02` is not a subclass of `AncileDemoGateway02` it does not compile. – Sebastian May 08 '17 at 22:50
4

Unfortunately, for some mysterious reason, that's probably inherited from the wonderful design of Java, you cannot extend more than one class either directly or indirectly. It is possible to mix in as many traits as you want, but you can only have one superclass in the hierarchy (ok, technically, you can have more than one, but they all must be extending each other - that's why your error message is complaining about SprayBoot not being a subclass).

In your case, you are extending SprayCanBoot02, which is a class, and also RestInterface02, that extends AncileDemoGateway02, which is also a class. So MyBoot02 is trying to extend two different classes at once, which is illegal.

Making AncileDemoGateway02 a trait should fix the error.

Dima
  • 39,570
  • 6
  • 44
  • 70