'If I create a class in Scala, does it implicitly extends AnyVal, AnyRef or both?
If I write a class in Scala, does it automatically extends AnyVal, AnyRef or both?
Solution 1:[1]
If I write a class in Scala, does it automatically extends AnyVal, AnyRef or both?
It should be obvious that it cannot automatically extend both because a Scala class can only extend one class, not two.
It should also be obvious that it cannot automatically extend AnyVal because classes that extend AnyVal must obey some specific restrictions which cannot, in general, be satisfied by any random class.
Therefore, the only sensible way it could work is that a class automatically extends AnyRef.
However, we don't have to use common sense, we don't have to use our brains at all, we can just look into the Scala Language Specification [bold emphasis mine]:
5.3 Class Definitions
The extends clause
extends sc with mt_1 with … with mt_mcan be omitted, in which caseextends scala.AnyRefis assumed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jörg W Mittag |
