我想做这样的事情(示例已简化,但包含所有关键部分):
class Master
{
type DataType = Int
var counter : DataType = 0
}
class Slave(private val master : Master)
{
val counter = master.counter // (*)
}
这里(*)我收到错误:
private value master escapes its defining scope as part of type Slave.this.master.DataType
val counter = master.counter
我理解错误,但不明白原因 - 该类型是类 Master
的一部分,而不是对象 master
的一部分,因此如果该类是私有(private)的,而不是对象。嗯,至少在理论上是这样。
很容易做出快速解决方法:
val counter : Master#DataType = master.counter
但我相信这是与以前完全相同的代码的显式版本,它“只是”需要更多的输入。那么这是一个功能吗?
问题:
Scala 中的类型(此处为 DataType)可以依赖于对象,而不是类(即每个类实例的类型定义)吗?
最佳答案
你这样想就错了
this is an explicit version of the exactly same code as before
Master#DataType
和 master.DataType
是两种不同的类型。
master.DataType
是以 master
作为外部对象的 DataType
实例的类型。换句话说,正是您所要求的,但显然 master
是类型的一部分,并且如果 master
则无法公开该类型不是。
Master#DataType
是任何外部对象的任何 DataType
实例的类型(相当于 Java 中的 Master.DataType
)。
回复评论:
类型成员可以在子类(包括仅包含一个对象的匿名子类)中重写,但只能由兼容类型重写。在您的示例中,DataType
已经在 Master
中具体化,因此唯一与其兼容的类就是它本身。所以类似
val a = new Master {
override type DataType = String
}
不会进行类型检查,这是有道理的:你会得到var counter: String = 0
,这是无意义的。但是
val a = new Master {
override type DataType = Int
}
可以工作(但不太有用)。
因此,只有重写抽象类型成员才有意义。但它们的类型检查方式与内部类相同,因此 a.DataType 通常不被认为与 b.DataType 相同——即使它们不能实际上是不同的!
关于scala - 谜题——用自定义类型公开私有(private)成员的公共(public)子成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8695452/