scala 类构造函数中的嵌套特征
我正在玩 scala (scala 2.8)。假设我有一个具有嵌套特征的类,并且想要使用该嵌套特征作为类构造函数中参数的类型。这可能吗?这是我最接近的:
class OuterClass(traitParam:OuterClass#InnerTrait) {
trait InnerTrait { }
val y:InnerTrait = traitParam
}
没有第三行甚至可以编译,但是一旦我尝试实际使用 traitParam
作为 InnerTrait
我就会收到编译器错误:
类型不匹配;发现:OuterClass#InnerTrait 需要:OuterClass.this.InnerTrait。
我不知道我能做什么(如果有的话)。相反
class OuterClass(traitParam:OuterClass.this.InnerTrait)
,正如错误消息可能暗示的那样,不会编译。除了将 InnerTrait
移至 OuterClass
之外,我还有其他选择吗?如果您想知道为什么我要这样做,答案是在我的实际代码中,OuterClass
的等效项具有类型参数,然后将在 InnerTrait
中使用这些类型参数。如果我将其移到外部,那么每次引用内部特征时都必须重新声明类型参数。
I'm playing around with scala (scala 2.8). Suppose I have a class with a nested trait, and want to use that nested trait as the type for a parameter in the class's constructor. Is that even possible? This is the closest I've come:
class OuterClass(traitParam:OuterClass#InnerTrait) {
trait InnerTrait { }
val y:InnerTrait = traitParam
}
Without the third line that even compiles, but as soon as I try to actually use the traitParam
as an InnerTrait
I get a compiler error:
type mismatch; found: OuterClass#InnerTrait required: OuterClass.this.InnerTrait.
I can't figure out what (if anything) I could do. Doing
class OuterClass(traitParam:OuterClass.this.InnerTrait)
instead, as the error message might suggest, does not compile. Do I have any choice other than to move InnerTrait
outside of OuterClass
? If you're wondering why I would want to do this, the answer is that in my actual code, the equivalent of OuterClass
has type parameters which would then be used in InnerTrait
. If I move it outside, then I have to restate the type parameters every time I reference the inner trait.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到了 Scala 的路径依赖类型。您的
val y: InnerTrait
的类型特定于它所在的实例。OuterClass#InnerTrait
是OuterClass
的所有实例的所有现有InnerTrait
的超类型。尝试使用这个:
You're encountering Scala's path-dependent types. your
val y: InnerTrait
's type is specific to the instance in which it's contained.OuterClass#InnerTrait
is a supertype of all theInnerTrait
extant for all instances ofOuterClass
.Try working with this:
因此可能有
a: OuterClass
和b: OuterClass
这样这些类型参数是不同的。例如:所以这是一个难题...
InnerTrait
必须绑定到OuterClass
的实例,因为每个实例可能有不同的类型参数。但是,您希望将InnerTrait
作为参数传递给OuterClass
构造函数,因此您需要在OuterClass
之前构造InnerTrait
代码>.但由于InnerTrait
必须绑定到OuterClass
的实例,因此OuterClass
必须在InnerClass
之前构造,从而将这变成了一只鸡&鸡蛋问题。这个设计有点奇怪,所以我建议你重新考虑一下。
So it is possible to have
a: OuterClass
andb: OuterClass
such that these type parameters are different. For instance:So here is the conundrum...
InnerTrait
must be tied to an instance ofOuterClass
, since each instance might have a different type parameter. However, you want to pass anInnerTrait
as parameter toOuterClass
constructor, so you'll need to constructInnerTrait
beforeOuterClass
. But sinceInnerTrait
has to be tied to an instance ofOuterClass
, thenOuterClass
must be constructed beforeInnerClass
, turning this into a chicken & egg problem.There's something strange with that design, so I suggest you rethink it through.