How to specialize on a type projection in Scala?

前端 未结 2 1270
情深已故
情深已故 2020-12-31 11:52

Statement of the question

Consider a type T that contains an abstract type member A:

trait T {
  type A
}
         


        
相关标签:
2条回答
  • 2020-12-31 12:33

    This is a compiler limitation; one cannot generally specialize on elements of a type parameter. However, the proposed trick is good enough for my purposes:

    trait Types {
      type A
      type B
    }
    
    trait GenOps[@specialized A, @specialized B] {
      ...
    }
    
    trait Ops[T <: Types] extends GenOps[T#A, T#B]
    

    This way the trait Ops gets specialized because it inherits the specialized implementations in trait GenOps. My motivation is that I want trait Ops to take a single type parameter T, rather than both T#A and T#B (this becomes necessary when Ops also takes a higher kinded type that expects T as a parameter).

    0 讨论(0)
  • 2020-12-31 12:40

    I can't see how that could possibly work. Specialization is done when compiling the class, and, at that time, A isn't known.

    0 讨论(0)
提交回复
热议问题