【问题标题】:Illegal inheritance, superclass X not a subclass of the superclass Y of the mixin trait Z - Scala非法继承,超类 X 不是 mixin 特征 Z 的超类 Y 的子类 - Scala
【发布时间】:2017-05-08 21:45:38
【问题描述】:

我正在尝试执行一个 akka-http,它是一个 scala 程序。我的 KISS 代码如下:-

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")
}

我得到的错误是因为我在尝试执行 MyBoot02 时接线的方式。错误如下:

错误:(58, 41) 非法继承;超类 SprayCanBoot 不是 mixin trait 的超类 AncileDemoGateway 的子类 RestInterface 对象 MyBoot 使用 RestInterface 扩展 SprayCanBoot 与应用程序

为什么错误状态为“SprayCanBoot 不是超类 AncileDemoGateway 的子类”。在我的代码中,SprayCanBoot 和 AncileDemoGateway 是 2 个独立的实体,那么为什么会出现这样的错误?

谢谢

【问题讨论】:

    标签: scala inheritance akka-http


    【解决方案1】:

    不幸的是,出于某种神秘的原因,这可能是继承自 Java 的出色设计,您不能直接或间接扩展多个类。 可以根据需要混合任意数量的特征,但层次结构中只能有一个超类(好吧,从技术上讲,你可以有多个,但它们都必须相互扩展 - 这就是你的错误消息的原因抱怨 SprayBoot 不是子类)。

    在您的情况下,您正在扩展SprayCanBoot02,它是一个类,还有RestInterface02,它扩展了AncileDemoGateway02,它也是一个类。所以MyBoot02 试图同时扩展两个不同的类,这是非法的。

    将AncileDemoGateway02 设为特征应该可以修复错误。

    【讨论】:

      【解决方案2】:

      一般来说,从一个类继承的 trait 通常用于限制一个 trait 可以混入的类。

      在您的情况下,MyBoot02 类不能扩展 RestInterface02 特征,因为 MyBoot02 和 RestInterface02 不共享同一个超类。

      【讨论】:

      • 您能否提供参考答案。我不认为这是这里的问题。谢谢
      • MyBoot02 的超类是SprayCanBoot02,RestInterface02 的超类是AncileDemoGateway02。由于SprayCanBoot02 不是AncileDemoGateway02 的子类,因此无法编译。
      猜你喜欢
      • 2013-10-23
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多