我正在尝试像代码一样实现蛋糕模式,但我得到了:
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]
使用以下代码。
可能是什么问题?
我查看了this SOF问题,但它似乎描述了一个不同的场景(或者我可能没有看到相似性)。
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)
}
答案 0 :(得分:3)
您承诺,特质Persistence
只会混合到一个扩展Persistable
:trait Persistence[State] { this:Persistable[State]
的类中。
相反,您的对象会延伸Persistence[State.State]
。这是一回事吗?