Scala隐含和覆盖问题
Scala implicits and overrides problems
这是我的问题:
trait Caller {
type EntityType
def parseEntity(entity: String): EntityType
}
trait IntCaller extends Caller {
implicit def strToInt(s: String) = s.toInt
override type EntityType = Int
override def parseEntity(entity: String): EntityType = entity
}
trait DoubleCaller extends Caller {
implicit def strToDouble(s: String) = s.toDouble
override type EntityType = Double
override def parseEntity(entity: String): EntityType = entity
}
object main {
def main(args: Array[String]): Unit = {
val intCaller = new IntCaller{}
val doubleCaller = new DoubleCaller{}
println("Result is: " + intCaller.parseEntity("5"))
println("Result is: " + doubleCaller.parseEntity("5.0"))
}
}
如您所见,我不断重复以下代码:parseEntity
方法。如果我想添加一个 FloatCaller
,我将不得不重写 parseEntity
,即使它的实现是相同的。
如何在 Caller
中编写 parseEntity 的实现,这样我就不必一次又一次地在子特征中编写相同的代码?
免责声明:
这是我对来自 akka.http.scaladsl.marshallers.sprayjson
.
的 SprayJsonSupport
的实际问题的简化
您最好使用可以在给定转换函数的情况下构建 Caller
实例的工厂方法。 IntCaller
和 DoubleCaller
之间唯一不同的是 toInt
和 toDouble
(当然还有类型)。
trait Caller {
type EntityType
def parseEntity(entity: String): EntityType
}
object Caller {
def apply[A](f: String => A): Caller = new Caller {
type EntityType = A
def parseEntity(entity: String): EntityType = f(entity)
}
}
scala> val IntCaller = Caller(_.toInt)
scala> IntCaller.parseEntity("123")
res1: IntCaller.EntityType = 123
scala> val DoubleCaller = Caller(_.toDouble)
scala> DoubleCaller.parseEntity("1.23")
res2: DoubleCaller.EntityType = 1.23
如果想继续使用继承,那就继续强制sub-类或traits实现与parseEntity
的转换。不过,实际上并不是必须使用隐式转换。出现重复代码的唯一原因是隐式转换使 parseEntity
对于每个实现看起来都一样,即使它实际上不是(因为它需要解析不同的隐式)。
trait Caller {
type EntityType
def parseEntity(entity: String): EntityType
}
trait IntCaller {
type EntityType = Int
def parseEntity(entity: String): EntityType = entity.toInt
}
这是我的问题:
trait Caller {
type EntityType
def parseEntity(entity: String): EntityType
}
trait IntCaller extends Caller {
implicit def strToInt(s: String) = s.toInt
override type EntityType = Int
override def parseEntity(entity: String): EntityType = entity
}
trait DoubleCaller extends Caller {
implicit def strToDouble(s: String) = s.toDouble
override type EntityType = Double
override def parseEntity(entity: String): EntityType = entity
}
object main {
def main(args: Array[String]): Unit = {
val intCaller = new IntCaller{}
val doubleCaller = new DoubleCaller{}
println("Result is: " + intCaller.parseEntity("5"))
println("Result is: " + doubleCaller.parseEntity("5.0"))
}
}
如您所见,我不断重复以下代码:parseEntity
方法。如果我想添加一个 FloatCaller
,我将不得不重写 parseEntity
,即使它的实现是相同的。
如何在 Caller
中编写 parseEntity 的实现,这样我就不必一次又一次地在子特征中编写相同的代码?
免责声明:
这是我对来自 akka.http.scaladsl.marshallers.sprayjson
.
SprayJsonSupport
的实际问题的简化
您最好使用可以在给定转换函数的情况下构建 Caller
实例的工厂方法。 IntCaller
和 DoubleCaller
之间唯一不同的是 toInt
和 toDouble
(当然还有类型)。
trait Caller {
type EntityType
def parseEntity(entity: String): EntityType
}
object Caller {
def apply[A](f: String => A): Caller = new Caller {
type EntityType = A
def parseEntity(entity: String): EntityType = f(entity)
}
}
scala> val IntCaller = Caller(_.toInt)
scala> IntCaller.parseEntity("123")
res1: IntCaller.EntityType = 123
scala> val DoubleCaller = Caller(_.toDouble)
scala> DoubleCaller.parseEntity("1.23")
res2: DoubleCaller.EntityType = 1.23
如果想继续使用继承,那就继续强制sub-类或traits实现与parseEntity
的转换。不过,实际上并不是必须使用隐式转换。出现重复代码的唯一原因是隐式转换使 parseEntity
对于每个实现看起来都一样,即使它实际上不是(因为它需要解析不同的隐式)。
trait Caller {
type EntityType
def parseEntity(entity: String): EntityType
}
trait IntCaller {
type EntityType = Int
def parseEntity(entity: String): EntityType = entity.toInt
}