简体   繁体   中英

illegal inheritance; self-type … does not conform to

I am trying to implement a cake pattern like code, but I get:

Error:(47, 36) illegal inheritance;
 self-type app.server.im.Im_Api_Service_Impl.type does not conform to app.server.im.Persistence[app.server.im.State.State]'s selftype app.server.im.Persistence[app.server.im.State.State] with app.server.im.Persistable[app.server.im.State.State]
object Im_Api_Service_Impl extends Persistence[State.State]

with the code below.

What might be the problem ?

I looked at this SOF question, but it seems to describe a different scenerio (or maybe I am just not seeing the similarity).

import app.shared.IndexChange
import app.shared.apiAndModel.im.{CanCreateEntity, Im_Api_Interface, LineShared, LineSharedPayload, UUID}
import upickle.default._
object State {
  type State = Seq[LineShared]
}

trait  Im_Api_Service extends Im_Api_Interface with Persistable[State.State] {
  implicit object CanCreate extends CanCreateEntity
  import State.State
  val init = List("egy", "ketto", "harom", "negy", "ot", "hat", "het", "nyolc").map(LineSharedPayload(_)).map(LineShared(_))

  val fileNameForPersistence="state"

  var state: State = init

  def setState(s:State)={state=s}

  override def getLines(): Seq[LineShared] = state


  def moveLine(ls: State, from: Int, to: Int): Seq[LineShared] = {
    val r=IndexChange(from, to).updatedList[LineShared](ls.toList)
    r
  }

  override def moveLine(from: Int, to: Int): Seq[LineShared] = {state=moveLine(state, from, to);state;}

  override def newLine(pl: LineSharedPayload): Seq[LineShared] = {
    setState(state :+ LineShared(pl))
    state
  }

  def getLine(ls:State, id: UUID): Option[LineShared] = ls.find(_.id == id)

  override def getLine(id: UUID): Option[LineShared] = getLine(state,id)


  override def updateLine(l: LineShared): State = {
    setState(state.map(ll => if (ll~l) l else ll ))
    state
  }
}

object Im_Api_Service_Impl extends Persistence[State.State]

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
               illegal inheritance problem HERE


trait Persistable[State]
{
  val fileNameForPersistence:String
  var state :State

}

trait Persistence[State] { this:Persistable[State] =>
  def serializeState(s: State): String = write(s)

  // TODO add timestamp to filename
  def saveState(): Unit = writeToFile(serializeState(state), fileNameForPersistence)

  def loadState(): Unit = {
    try {
      state = deserializeState(readFromFile(fileNameForPersistence))
    }
    catch {
      case e: Exception => println(e)
    }
  }

  def writeToFile(s: String, fn: String): Unit = {
    import java.io._
    val pw = new PrintWriter(new File(fn))
    pw.write(s)
    pw.close
  }


  def readFromFile(fn: String): String = {
    val source = scala.io.Source.fromFile(fn)
    val lines: String = try source.mkString finally source.close()
    lines
  }

  def deserializeState(s: String): State = read[State](s)
}

You have promised that trait Persistence would only be mixed into a class that extends Persistable : trait Persistence[State] { this:Persistable[State] .

Instead, your object extends Persistence[State.State] . Is that the same thing?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM