简体   繁体   中英

override lazy val in constructor / scala

I want to override a lazy val in the constructor in scala. Any ideas how to do this ? I tried the following but i get

'lazy' modifier not allowed here, use call-by-name parameter instead

  class Dog(override lazy val creatureType: String) extends Animal {

// valid
//  override lazy val creatureType: String = "Dog"
// but i want to override it in the constructor directly 

}

The Parent class :

  class Animal {
 lazy val creatureType: String = "unknown"
  }

Something like this should work:

   class Dog(foo: => String) extends Animal {
     override lazy val creatureType = foo
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM