自类型注释阻碍了内部类的实例化。为什么?
给定 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于
Inner1
,自类型表示它始终会与抽象类型Inner_Tp
一起实例化。这是一个在实例化点未实现的承诺:类型仅为Inner1
,而不是Inner1 with Inner_Tp
。如果您需要延迟
Inner_Tp
的定义,则还需要延迟创建任何将其作为自类型的实例。这是绝对必要的,因为您无法生成您还不知道的类型的值。所以最好让方法保持抽象。您也可以改进抽象类型:我不确定您想要什么,但您可能根本不需要自我类型(为了简洁起见,我已经将它们排除在外)。
For
Inner1
, the self-type says that it will always be instantiated together with the abstract typeInner_Tp
. That's a promise that is not fulfilled at the instantiation point: the type is onlyInner1
instead ofInner1 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: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).
因为
self: Inner_Tp
告诉Inner
的任何子类型都不允许被实例化,除非它也是Inner_Tp
的子类型。重写
self =>
只是重申了约束,但并不满足它。为了得到类似的想法,当你有一个抽象类的后代时,如果它没有实现抽象方法,你必须再次写它是抽象的。而且你无法实例化。同样,您重申了Inner1
中的 Inner_TpInner_Tp
是必需的,但您没有使Inner_Tp
可用。只要
Inner_Tp
是抽象类型,您就无法将其混合进去,因此您将无法编写new Inner1
。您可能应该引入一个创建 Inners 的抽象方法。Because the
self: Inner_Tp
tells that no subtype ofInner
will be allowed to be instanciated unless it is a subtype ofInner_Tp
too.Rewriting
self => Inner_Tp
inInner1
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 thatInner_Tp
is required, you did not makeInner_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 writenew Inner1
. You should probably introduce an abstract method that create Inners.