Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are all the overriding rules between def, val, lazy val, and var in scala?

In scala, there are four kinds of member modifier, i.e. def, val, lazy val, var. There is a seemingly complicated and inconsistent set of rules regarding overriding, for instance, def can be overridden by val, while not the other way around. It would be great to see a full list of all these rules.

like image 371
Lifu Huang Avatar asked Dec 07 '22 20:12

Lifu Huang


1 Answers

All overridings in scala fall into two categories, the first case is to override an abstract member(in trait or abstract class) and the other is to override a concrete member:

Override concrete members

def, val, lazy val, var might all be overridden in subclasses:

  1. def: can be overridden by all kinds of members(def, val, lazy val, and var).

  2. val: can only be overridden by val.

  3. lazy val: can only be overridden by lazy val.

  4. var: a concrete var cannot be overridden.

Override abstract members

lazy val cannot be abstract, so there are only three rules:

  1. def: can be overridden by all kinds of members(def, val, lazy val, and var).

  2. val: can be overridden by val and lazy val.

  3. var: can be overridden by (1) var, or (2) a pair of read and write operations implemented by def, val, or lazy val.

EDITED:

As pointed out by @iuriisusuk, please refer to the related section in spec for a formal description.

like image 164
Lifu Huang Avatar answered Dec 14 '22 23:12

Lifu Huang