为什么这些类型参数不符合类型细化? [英] Why do these type arguments not conform to a type refinement?
问题描述
为什么这段 Scala 代码无法进行类型检查?
Why does this Scala code fail to typecheck?
trait T { type A }
trait GenFoo[A0, S <: T { type A = A0 }]
trait Foo[S <: T] extends GenFoo[S#A, S]
我不明白为什么类型参数 [S#A,S] 不符合 trait GenFoo 的类型参数边界 [A0,S <: T{type A = A0}]".有解决办法吗?
I don't understand why "type arguments [S#A,S] do not conform to trait GenFoo's type parameter bounds [A0,S <: T{type A = A0}]". Is there a work-around?
编辑:正如已经指出的,一致性错误源于未能验证S <: T{type A = S#A}
.Daniel Sobral 指向 -explaintypes
,它告诉我们:
Edit: As has been pointed out, the conformance error stems from the failure to verify S <: T{type A = S#A}
. Daniel Sobral pointed to -explaintypes
, which tells us:
S <: T{type A = S#A}?
S <: T?
true
S specializes type A?
this.A = this.A?
S = this.type?
false
false
false
false
我不知道如何解释这个.
I'm not sure how to interpret this.
注意,如果我们尝试定义,我们会得到一个非法的循环引用,
Note that we get an illegal cyclic reference if we try to define,
trait Foo[S <: T { type A = S#A } ] extends GenFoo[S#A, S]
虽然这里的类型细化似乎没有添加任何新信息.(另见 为什么这个循环引用带有类型投影非法吗?)
although the type refinement here doesn't seem to add any new information. (See also Why is this cyclic reference with a type projection illegal?)
我的动机是创建一个特性 Foo[S <: T]
专门用于 S#A
,如:如何专注于 Scala 中的类型投影? 为了让它起作用,我我试图将 S#A
作为显式参数 A0
显示在实现特征 GenFoo
中,它可以直接特化.我希望将 Miles Sabin 的回答中的类型细化思想应用到 为什么这个带有类型投影的循环引用是非法的? 但我遇到了这个一致性错误.
My motivation is to create a trait Foo[S <: T]
that specializes on S#A
, as in: How to specialize on a type projection in Scala? To get this to work, I'm trying to surface S#A
as an explicit parameter A0
in the implementation trait GenFoo
, which can be specialized directly. I was hoping to apply the type refinement idea from Miles Sabin's answer to Why is this cyclic reference with a type projection illegal? but I run into this conformance error.
推荐答案
这似乎就是答案:
S 专攻 A 型?
关于specialize 的问题来自这里:T { type A = A0 }
.这是带有type A
specialized 的T
类型——意思是,它比原来的T
更受限制.
The question about specialize comes from here: T { type A = A0 }
. This is the type T
with type A
specialized -- meaning, it is more restricted than the original T
.
这个问题的答案是否定的——S
没有限制它是专门的.
The answer to that question is no -- there's no constrains on S
that it be specialized.
这篇关于为什么这些类型参数不符合类型细化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!