Select all correct statements about val form and def form definitions:
Explanation
Get an explanation when it's available:
Theory
  • Function parameters can be passed by value or be passed by name.
    The same distinction applies to definitions.
    The def form is “by-name”, its right hand side is evaluated on each use.
    There is also a val form, which is “by-value”.
    Example:
    val x = 2
    val y = square(x)
    
    The right-hand side of a val definition is evaluated at the point of the definition itself.
    Afterwards, the name refers to the value. For instance, y above refers to 4, not square(2).
  • The difference between val and def becomes apparent when the right hand side does not terminate.
    Given
    def loop: Boolean = loop
    
    A definition
    def x = loop
    
    is OK, but a definition
    val x = loop
    
    will lead to an infinite loop.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Scala
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести