Why is this cyclic reference with a type projection illegal?

前端 未结 1 1702
忘了有多久
忘了有多久 2020-12-14 10:20

The following pseudo-Scala yields an \"illegal cyclic reference\" error:

trait GenT[A]
trait T extends GenT[T#A] {
  type A
}

Quest

相关标签:
1条回答
  • 2020-12-14 11:08

    In your initial example you can break the cycle by introducing an auxiliary type inbetween GenT[A] and T,

    trait GenT[A]
    trait TAux { type A }
    trait T extends TAux with GenT[TAux#A]
    

    But from your motivating example I don't think you need to go down this route. The constraint you're after can be expressed directly using a refinement,

    trait T { type A }
    def foo[A0, S1 <: T { type A = A0 }, S2 <: T { type A = A0 }] ...
    

    Also bear in mind that you can surface a type member as a type parameter via a type alias,

    trait T { type A }
    type TParam[A0] = T { type A = A0 }
    def foo[A0, S1 <: TParam[A0], S2 <: TParam[A0]] ...
    
    0 讨论(0)
提交回复
热议问题