scala伴随对象可以有抽象成员吗



EDIT:使问题更清楚

我有一个函数试图将Map[String, String]解析为ReadRequestsWriteRequests。我需要从抽象类Request隐式地访问MapConvert

// Conversion methods to make conversions from Map[String, String] to ReadRequest and WriteRequest implicit
trait MapConvert[A] {
def convert(values: Map[String, String]): A
}
object Map2ClassHelpers {
implicit class Map2Class(values: Map[String, String]) {
def extract[A](implicit mapper: MapConvert[A]): A = mapper convert values
}
}
// abstract Request class
abstract class Request[R <: Request[R]]() {
def companion: RequestCompanion[R]
}
trait RequestCompanion[R <: Request[R]] {
implicit val m: MapConvert[R]
}
// ReadRequest and companion object
case class ReadRequest(source: String, data: String)
extends Request[ReadRequest] {
override def companion: RequestCompanion[ReadRequest] = ReadRequest
}
object ReadRequest extends RequestCompanion[ReadRequest] {
implicit val m = new MapConvert[ReadRequest] {
def convert(values: Map[String, String]) = ReadRequest(
source = values("source"),
data = values("data")
)
}
}
// WriteRequest and companion object
case class WriteRequest(destination: String, data: String)
extends Request[WriteRequest] {
override def companion: RequestCompanion[WriteRequest] = WriteRequest
}
object WriteRequest extends RequestCompanion[WriteRequest] {
implicit val m = new MapConvert[WriteRequest] {
def convert(values: Map[String, String]) = WriteRequest(
destination = values("destination"),
data = values("data")
)
}
}
case class Spec(requestType: String, args: Map[String, String])
def fromMapToRequest[R <: Request[R]](spec: Spec)(implicit mR: Manifest[R]): R = {
/* this extract method comes from Map2ClassHelpers
* the result should be either a ReadRequest or a WriteRequest
* the error happens at this call saying "could not find implicit value for parameter mapper
* my guess is because it can't implicitly find the correct MapConvert from the abstract Request class
*/
// a bunch of checks ...
spec.args.extract[R]
}
def runExample() = {
val spec1: Spec = Spec("read", Map("source" -> "/readPath", "data" -> "ABC"))
val spec2: Spec = Spec("write", Map("destination" -> "/writePath", "data" -> "ABC"))
val specs = Seq(spec1, spec2)
specs.map(spec => {
spec.requestType match {
case "read" => fromMapToRequest[ReadRequest](spec)
case "write" => fromMapToRequest[WriteRequest](spec)
}
})
}

当我把它改为:时,它就起作用了

def fromMapToRequest[R <: Request[R]](spec: Spec)(eval: spec => Request)(implicit mR: Manifest[R]): R = {
// a bunch of checks ...
import Map2ClassHelpers._
eval(spec)
}
def runExample() = {
val spec1: Spec = Spec("read", Map("source" -> "/readPath", "data" -> "ABC"))
val spec2: Spec = Spec("write", Map("destination" -> "/writePath", "data" -> "ABC"))
val specs = Seq(spec1, spec2)
import Map2ClassHelpers._
specs.map(spec => {
// this part is kind of ugly compared to the first way
spec.requestType match {
case "read" => fromMapToRequest[ReadRequest](spec)(_.args.extract[ReadRequest])
case "write" => fromMapToRequest[WriteRequest](spec)(_.args.extract[WriteRequest])
}
})
}

但我想要更通用的第一个选项,所以只需要在fromMapToRequest中指定一次类型。

我不确定我是否正确理解您的问题,但如果我理解了,问题似乎是您试图传递implicit Manifest[R],希望编译器能够在伴随对象中找到正确的MapConvert[R],而这种方法不起作用,因为Manifest没有扩展implicit参数的查找范围。但是,为什么不能将MapConvert[R]本身作为implicit参数传递到fromMapToRequest中呢?当我将签名更改为时

def fromMapToRequest[R <: Request[R]](spec: Spec)(implicit mc: MapConvert[R]): R = {

代码按照我所期望的方式编译和工作(请在线查看(。如果您的其他检查需要Manifest[R],只需传递两个implicit参数即可。

(可能由于问题编辑而过时(

定义伴随对象的特征:

trait FoobarCompanion[X <: Foobar[X]] {
implicit val m: Converter[X]
}

让伴随对象扩展它:

object Foo extends FoobarCompanion[Foo] {
implicit val m = Converter[Foo] { ... }
}
object Bar extends FoobarCompanion[Bar] {
implicit val m = Converter[Bar] { ... }
}

将方法companion添加到Foobar本身:

abstract class Foobar[F <: Foobar[F]] {
def companion: FoobarCompanion[F]
}

现在,无论何时通过companion获得Foobar的实例,都可以访问正确的隐式。

您可能想看看GenericCompanion周围的代码是如何组织的。

相关内容

  • 没有找到相关文章

最新更新