2

I am just trying to learn SORM and am playing with what I think is some simple sample code. To whit:

case class Book(var author:String, var title:String);

object Db extends Instance(
  entities = Set(Entity[Book]()),
  url = "jdbc:h2:mem:test",
  user = "",
  password = "",
  initMode = InitMode.Create
)

And then:

val b : Book with Persisted = Db.save( Book("foo","bar")  )

When attempting to compile this, I get:

[error] /Users/rjf/IdeaProjects/PlayTest/app/controllers/Application.scala:22: type mismatch;
[error]  found   : models.Book
[error]  required: sorm.Persisted with models.Book
[error]     val b : Book with Persisted = Db.save( Book("foo","bar")  )
[error]                                                ^

If I change the Book declaration to:

case class Book(var author:String, var title:String) extends Persisted;

I then get:

[error] /Users/rjf/IdeaProjects/PlayTest/app/models/Book.scala:17: class Book needs to be abstract, since:
[error] it has 2 unimplemented members.
[error] /** As seen from class Book, the missing signatures are as follows.
[error]  *  For convenience, these are usable as stub implementations.
[error]  */
[error]   def id: Long = ???
[error]   def mixoutPersisted[T]: (Long, T) = ???
[error] case class Book(var author:String, var title:String) extends Persisted;
[error]            ^

Apologies for the newbie question. No doubt that the fact I'm learning Scala at the same time is a contributing factor.

4

1 回答 1

1

要解决您的问题,只需删除显式类型注释:

val b = Db.save( Book("foo","bar") )

不用担心,您仍然可以访问与Book with Persisted.

至于为什么会出现这种情况,这似乎是 Scala 的一个 bug,并且有报道

旁注

不要var在 case 类声明中使用 s。不变性是案例类的重点。正确的声明是:

case class Book(author:String, title:String)

这与

case class Book(val author:String, val title:String)
于 2013-11-17T13:47:41.327 回答