自类型注释阻碍了内部类的实例化。为什么?

发布于 2024-12-17 09:33:50 字数 650 浏览 3 评论 0原文

给定 Outer 类及其 Inner 类的抽象定义,我想实例化 Outer1 中定义的具体 Inner1 类代码>特征。

abstract class Outer {
  type Inner_Tp <: Inner;
  abstract class Inner {
    self: Inner_Tp =>
  }
}

trait Outer1 {
  self: Outer =>
  protected class Inner1 extends Inner {
    self: Inner_Tp =>
  }
  def Inner1() = new Inner1()
}

Scala 编译器提前终止编译,并给出以下错误消息:“错误:类 Inner1 无法实例化,因为它与 Outer1.this.Inner_Tp 不符合其自身类型 Outer1.this.Inner1”。 为什么?

毕竟 Inner1 类是在抽象上下文中定义的,这是它的 Outer1 特征。我想推迟 type Inner_Tp 的定义,直到该特征混合到某个具体类中。

Given the abstract definitions of the Outer class and its Inner class I would like to instantiate the concrete Inner1 class defined within Outer1 trait.

abstract class Outer {
  type Inner_Tp <: Inner;
  abstract class Inner {
    self: Inner_Tp =>
  }
}

trait Outer1 {
  self: Outer =>
  protected class Inner1 extends Inner {
    self: Inner_Tp =>
  }
  def Inner1() = new Inner1()
}

The Scala compiler prematurely terminates the compilation giving me the following error message: "error: class Inner1 cannot be instantiated because it does not conform to its self-type Outer1.this.Inner1 with Outer1.this.Inner_Tp". Why?

After all the Inner1 class is defined within an abstract context being its Outer1 trait. I would like to postpone the definition of type Inner_Tp until the trait gets mixed into some concrete class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

叹倦 2024-12-24 09:33:50

对于 Inner1,自类型表示它始终会与抽象类型 Inner_Tp 一起实例化。这是一个在实例化点未实现的承诺:类型仅为 Inner1,而不是 Inner1 with Inner_Tp

如果您需要延迟 Inner_Tp 的定义,则还需要延迟创建任何将其作为自类型的实例。这是绝对必要的,因为您无法生成您还不知道的类型的值。所以最好让方法保持抽象。您也可以改进抽象类型:

trait Outer1 extends Outer {
   type Inner_Tp <: Inner1
   protected class Inner1 extends Inner

   def Inner1(): Inner_Tp
}

我不确定您想要什么,但您可能根本不需要自我类型(为了简洁起见,我已经将它们排除在外)。

For Inner1, the self-type says that it will always be instantiated together with the abstract type Inner_Tp. That's a promise that is not fulfilled at the instantiation point: the type is only Inner1 instead of Inner1 with Inner_Tp.

If you need to delay the definition of Inner_Tp, you also need to delay the creation of any instances that have it as a self-type. That is absolutely necessary, since you cannot produce a value of a type that you don't know yet. So better leave the method abstract. You may refine the abstract type as well:

trait Outer1 extends Outer {
   type Inner_Tp <: Inner1
   protected class Inner1 extends Inner

   def Inner1(): Inner_Tp
}

I'm not sure what you are after, but you may not need the self-types at all (I left them out for brevity already).

娇妻 2024-12-24 09:33:50

因为 self: Inner_Tp 告诉 Inner 的任何子类型都不允许被实例化,除非它也是 Inner_Tp 的子类型。

重写self => Inner1 中的 Inner_Tp 只是重申了约束,但并不满足它。为了得到类似的想法,当你有一个抽象类的后代时,如果它没有实现抽象方法,你必须再次写它是抽象的。而且你无法实例化。同样,您重申了 Inner_Tp 是必需的,但您没有使 Inner_Tp 可用。

只要 Inner_Tp 是抽象类型,您就无法将其混合进去,因此您将无法编写 new Inner1。您可能应该引入一个创建 Inners 的抽象方法。

Because the self: Inner_Tp tells that no subtype of Inner will be allowed to be instanciated unless it is a subtype of Inner_Tp too.

Rewriting self => Inner_Tp in Inner1 just restates the constraint, it does not satisfy it. To get a similar idea, when you have a descendant of an abstract class, if it does not implement the abstract method, you must write again that it is abstract. And you cannot instanciate. Same here, you restated that Inner_Tp is required, you did not make Inner_Tp available.

As long as Inner_Tp is an abstract type, you will not be able to mix it in, and so you will not be able to write new Inner1. You should probably introduce an abstract method that create Inners.

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