Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ask for the same type in Scala generics without introducing a third type parameter?

Tags:

generics

scala

Right now my trait has an extra type T which has no real usage other than to ensure B and R have the same generic 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 maintaining the type strictness.

EDIT: I don't have control of the Builder or Result code - they are pulled in from some java artifacts outside.

like image 688
NSF Avatar asked Nov 01 '22 15:11

NSF


1 Answers

If you just want to simplify that part of the declaration you could move the types into members, but then you end up having to cart around more information 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: B.T =:= R.T
}
like image 155
lmm Avatar answered Nov 15 '22 07:11

lmm