1

编译器说

error: ambiguous reference to overloaded definition,
both method unapply in object UserDto of type (in: Any)Option[(String, String, String, String)]
and  method unapply in class AbstractJsonConversion of type (in: net.liftweb.json.JsonAST.JValue)Option[biz.shopboard.domain.model.UserDto]
match argument types (net.liftweb.json.JsonAST.JValue)
case Nil JsonPut UserDto(user) -> _ => {

代码看起来像这样(Scala + Lift)

serve("api" / "user" prefix {
    // PUT adds the item if the JSON is parsable
    case Nil JsonPut UserDto(user) -> _ => {
      facade.saveUser(user)
      user: JValue
    }
}

case class UserDto(username: String, password: String, firstName: String, lastName: String)

object UserDto extends AbstractJsonConversion[UserDto] {
  /**
   * The default unapply method for the case class.
   * We needed to replicate it here because we
   * have overloaded unapply methods
   */
  def unapply(in: Any): Option[(String, String, String, String)] = {
    in match {
      case i: UserDto => Some((i.username, i.password, i.firstName, i.lastName))
      case _ => None
    }
  }

}

和 AbstractJsonConversion 看起来像这样

package biz.shopboard.domain

import net.liftweb.json.JsonAST.JValue
import net.liftweb.common.Box
import net.liftweb.util.Helpers
import net.liftweb.json.Extraction

class AbstractJsonConversion[T](implicit M: Manifest[T]) {

  implicit val formats = net.liftweb.json.DefaultFormats

  /**
   * Convert a JValue to a User if possible
   */
  def apply(in: JValue): Box[T] = Helpers.tryo { in.extract[T] }

  /**
   * Extract a JValue to an Item
   */
  def unapply(in: JValue): Option[T] = apply(in)

  /**
   * Convert the item to JSON format.  This is
   * implicit and in the companion object, so
   * an Item can be returned easily from a JSON call
   */
  implicit def toJson(item: T): JValue = Extraction.decompose(item)
}

如果我将重载的 unapply 方法从父类 AbstractJsonConversion 移动到伴随对象 UserDto 一切都很好。

我想知道我在这里做错了什么?为什么我不能在父类 AbstractJsonConversion 中使用重载方法 unapply 而在它的子伴随对象中使用另一个方法?

4

0 回答 0