简体   繁体   中英

Changing Scala lazy val behaviour

Suppose I have a String type lazy val :

Class LazyVals { 

     lazy val message = "I am lazy on + " + System.currentTimeMillis().toString    

     def changeLazy = { message + " Not!!" }
    }

Will changeLazy change the "evaluate once only" nature of message?

Code to calculate message will be evaluated once only. You can't change this behavior.

currentTimeMillis will be called only once. Concatenation message + " Not!!" will be performed on each changeLazy call with the same result, but can be optimized by jvm .

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