1

In the Slick library we find this declaration:

/** 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
}

What does P#Backend#Database mean? What does # do? Is it a standard Scala construct or something from a library?

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    [Path dependent types](https://www.artima.com/pins1ed/abstract-members.html#20.7). `Database` is a type defined in `Backend`, and `Backend` is a type defined in `P`. – jwvh Apr 27 '19 at 06:51

1 Answers1

2

The # symbol in a type declaration is similar to a . in normal code: it refers to a type defined inside a type. Here is a simplified example:

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
}

If we change the type of db to something else:

case object MyConfig extends Config[MyProfile] {
  def db = 123
}

there is an error that explains what the type means:

type mismatch;
 found   : Int(123)
 required: MyProfile#Database
    (which expands to)  String
Koterpillar
  • 7,883
  • 2
  • 25
  • 41