I start slow migration from Java to Scala and facing issues. One of them is clumsy constructor arguments. I know I cannot avoid them, but at least I would like to introduce some sort of type alias like C++ has typename to be used in subclasses.
case class A[E <: Throwable]
(
mappers: Map[Class[_ :< Throwable], ExceptionMapper[_ <: Throwable]]
) {
}
case class B[CompletionException]
(
mappers: Map[Class[_ :< Throwable], ExceptionMapper[_ <: Throwable]]
) extends A(mappers) {
}
Run Code Online (Sandbox Code Playgroud)
So in sample above mappers field has horrible type and I would like to type it once in base class only.
在Scala中,您可以引入类型别名
type M = Map[Class[_ <: Throwable], ExceptionMapper[_ <: Throwable]]
class A[E <: Throwable]
(
mappers: M
) {
}
class B[CompletionException]
(
mappers: M
) extends A(mappers) {
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,由于/ 的问题,案例类不应相互扩展。equalshashCode
| 归档时间: |
|
| 查看次数: |
45 次 |
| 最近记录: |