在Slick库中,我们找到以下声明:
/** A configuration for a Database plus a matching Profile. */
trait DatabaseConfig[P <: BasicProfile] {
/** Get the configured Database. It is instantiated lazily when this method is called for the
* first time, and must be closed after use. */
def db: P#Backend#Database
}
P#Backend#Database
是什么意思? #
是做什么的?它是标准的Scala构造还是来自库的东西?
答案 0 :(得分:1)
类型声明中的#
符号与普通代码中的.
类似:它引用在类型内部定义的 type 。这是一个简化的示例:
trait Profile {
type Database
}
trait Config[P <: Profile] {
def db: P#Database
}
case class MyProfile() extends Profile {
type Database = String
}
case object MyConfig extends Config[MyProfile] {
def db = "mydatabase" // has the type MyProfile#Database, that is, String
}
如果我们将db
的类型更改为其他类型:
case object MyConfig extends Config[MyProfile] {
def db = 123
}
有一个错误解释了类型的含义:
type mismatch;
found : Int(123)
required: MyProfile#Database
(which expands to) String