`def` vs `val` vs `lazy val` evaluation in Scala

前端 未结 8 985
再見小時候
再見小時候 2020-11-30 19:05

Am I right understanding that

  • def is evaluated every time it gets accessed

  • lazy val is evaluated once it gets acce

8条回答
  •  甜味超标
    2020-11-30 19:50

    One good reason for choosing def over val, especially in abstract classes (or in traits that are used to mimic Java's interfaces), is, that you can override a def with a val in subclasses, but not the other way round.

    Regarding lazy, there are two things I can see that one should have in mind. The first is that lazy introduces some runtime overhead, but I guess that you would need to benchmark your specific situation to find out whether this actually has a significant impact on the runtime performance. The other problem with lazy is that it possibly delays raising an exception, which might make it harder to reason about your program, because the exception is not thrown upfront but only on first use.

提交回复
热议问题