What is the syntax meaning of “`class declaration head` { val_name : Type => `class body` }”

后端 未结 3 404
遥遥无期
遥遥无期 2021-01-18 05:11

When reading some articles about Scala, I found some examples with a curious syntax, which I might understand incorrectly

class Child[C <: Child[C]]         


        
3条回答
  •  感情败类
    2021-01-18 05:41

    In addition to the inheritance requirement in JaimeJorge's response, self types can be used to give the outer instance a name if you want to refer to it from an inner class:

    scala> class Company(name: String) {
         |   company =>
         |   class Department(name: String) {
         |     override def toString = "Department "+ name +" of "+ company.name
         |   }
         | }
    defined class Company
    
    scala> val c = new Company("ACME")
    c: Company = Company@56a57bb2
    
    scala> val d = new c.Department("Marketing")
    d: c.Department = Department Marketing of ACME
    

提交回复
热议问题