212
212

Reputation: 985

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?

Upvotes: 0

Views: 180

Answers (1)

senia
senia

Reputation: 38045

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.

Upvotes: 3

Related Questions