How to set the same type in Scala generators without entering a parameter of the third type?

Currently my trait has an additional type T, which has no real use, except to ensure that B and R have the same common type.

trait GenericBuilder[T <: Any, B <: Builder[T], R <: Result[T]]

To simplify the declaration, I wonder if there is a way to eliminate T in this case, while preserving type rigor.

EDIT: I do not control the Builder or Result code - they are pulled from some Java artifacts outside.

+5
source share
1 answer

If you just want to simplify this part of the declaration, you can move the types to members, but then you have to go around the cart more because you need a witness that the types are the same:

 trait Builder { type T } trait Result { type T } trait GenericBuilder[B <: Builder, R <: Result] { val w: BT =:= RT } 
+2
source

Source: https://habr.com/ru/post/1204617/


All Articles