2

我有这个域对象:

case class Person (
  name : String,
  age: Option[Int],
  email : String
) extends MongoObject

带有年龄可选字段。所以我定义了我的工厂:

object Person extends MongoObjectShape[Person] {
  lazy val name = Field.scalar("name", _.name)
  lazy val age = Field.optional("age", t => t.age)
  lazy val email = Field.scalar("email", _.email)

  override lazy val * = name :: age :: email :: Nil
  override def factory(dbo: DBObject): Option[Person] = 
    for {
      n <- name from dbo
      t <- age from dbo
      z <- email from dbo
    } yield new Person(n, t, z)
}

但它不编译,因为我得到:

[error]  found   : Int
[error]  required: Option[Int]
[error]     } yield new Person(n, t, z)
[error]                           ^

这有什么问题?

4

3 回答 3

4

也许,你可以写

override def factory(dbo: DBObject): Option[Person] = 
    for {
      n <- name from dbo
      z <- email from dbo
    } yield new Person(n, age from dbo, z)
于 2010-12-17T16:54:20.827 回答
3

amsayk 答案的变体,但稍微对称

for {
      n <- name from dbo
      t = age from dbo
      z <- email from dbo
    } yield new Person(n, t, z)
于 2010-12-19T17:47:40.463 回答
1

您需要将 t 包装在 Some 对象中

} yield new Person(n, Some(t), z)
于 2010-12-17T17:15:45.417 回答