scala 类构造函数中的嵌套特征

发布于 2024-09-17 16:32:14 字数 753 浏览 1 评论 0原文

我正在玩 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一念一轮回 2024-09-24 16:32:14

您遇到了 Scala 的路径依赖类型。您的 val y: InnerTrait 的类型特定于它所在的实例。 OuterClass#InnerTraitOuterClass 的所有实例的所有现有 InnerTrait 的超类型。

尝试使用这个:

class OuterClass(traitParam: OuterClass#InnerTrait) {
    trait InnerTrait { }

    type IT = OuterClass#InnerTrait

    def m1: IT = traitParam
}

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 the InnerTrait extant for all instances of OuterClass.

Try working with this:

class OuterClass(traitParam: OuterClass#InnerTrait) {
    trait InnerTrait { }

    type IT = OuterClass#InnerTrait

    def m1: IT = traitParam
}
ゃ懵逼小萝莉 2024-09-24 16:32:14

OuterClass 具有类型参数,
然后将在 InnerTrait 中使用

因此可能有 a: OuterClassb: OuterClass 这样这些类型参数是不同的。例如:

abstract class OuterClass[T] {
  val x: T
}

val a = new OuterClass[Int] { val x = 5 }
val b = new OuterClass[String] { val x = "abc" }

所以这是一个难题... InnerTrait 必须绑定到 OuterClass 的实例,因为每个实例可能有不同的类型参数。但是,您希望将 InnerTrait 作为参数传递给 OuterClass 构造函数,因此您需要在 OuterClass 之前构造 InnerTrait代码>.但由于 InnerTrait 必须绑定到 OuterClass 的实例,因此 OuterClass 必须在 InnerClass 之前构造,从而将这变成了一只鸡&鸡蛋问题。

这个设计有点奇怪,所以我建议你重新考虑一下。

OuterClass has type parameters which
would then be used in InnerTrait

So it is possible to have a: OuterClass and b: OuterClass such that these type parameters are different. For instance:

abstract class OuterClass[T] {
  val x: T
}

val a = new OuterClass[Int] { val x = 5 }
val b = new OuterClass[String] { val x = "abc" }

So here is the conundrum... InnerTrait must be tied to an instance of OuterClass, since each instance might have a different type parameter. However, you want to pass an InnerTrait as parameter to OuterClass constructor, so you'll need to construct InnerTrait before OuterClass. But since InnerTrait has to be tied to an instance of OuterClass, then OuterClass must be constructed before InnerClass, turning this into a chicken & egg problem.

There's something strange with that design, so I suggest you rethink it through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文