How avoid type repetition for inherited fields in Scala

Dan*_*kov 1 java types scala

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.

Dmy*_*tin 6

在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