|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: super traits |
| 4 | +--- |
| 5 | + |
| 6 | +Traits are used in two roles: |
| 7 | + |
| 8 | + 1. As mixins for other classes and traits |
| 9 | + 2. As types of vals, defs, or parameters |
| 10 | + |
| 11 | +Some traits are used primarily in the first role, and we usually do not want to see them in inferred types. An example is the `Product` trait that the compiler |
| 12 | +adds as a super trait to every case class or case object. In Scala 2, this parent trait sometimes makes inferred types more complicated than they should be. Example: |
| 13 | +```scala |
| 14 | +trait Kind |
| 15 | +case object Var extends Kind |
| 16 | +case object Val extends Kind |
| 17 | +val x = Set(if condition then Val else Var) |
| 18 | +``` |
| 19 | +Here, the inferred type of `x` is `Set[Kind & Product & Serializable]` whereas one would have hoped it to be `Set[Kind]`. The reasoning for this particular type to be inferred is as follows: |
| 20 | + |
| 21 | + - The type of the conditional above is the union type `Val | Var`. |
| 22 | + - A union type is widened in type inference to the least supertype that is |
| 23 | + not a union type. In the example, this type is `Kind & Product & Serializable` since all three traits are supertraits of both `Val` and `Var`. |
| 24 | + So that type becomes the inferred element type of the set. |
| 25 | + |
| 26 | +Scala 3 allows one to mark a trait as a `super` trait, which means that it can be suppressed in type inference. Here's an example that follows the lines of the |
| 27 | +code above, but now with a new super trait `S` instead of `Product`: |
| 28 | +```scala |
| 29 | +super trait S |
| 30 | +trait Kind |
| 31 | +object Var extends Kind, S |
| 32 | +object Val extends Kind, S |
| 33 | +val x = Set(if condition then Val else Var) |
| 34 | +``` |
| 35 | +Now `x` has inferred type `Set[Kind]`. The common super trait `S` does not |
| 36 | +appear in the inferred type. |
| 37 | + |
| 38 | +### Super Traits |
| 39 | + |
| 40 | +The traits `scala.Product`, `java.lang.Serializable` and `java.lang.Comparable` |
| 41 | +are treated automatically as super traits. Other traits can be turned into super traits, by adding the keyword `super` in front of `trait`, as shown above. |
| 42 | + |
| 43 | +Every trait can be declared as a super trait. Typically super traits are traits that influence the implementation of inheriting classes and traits and that are not usually used as types by themselves. Two examples from the |
| 44 | +standard collection library: |
| 45 | + |
| 46 | + - `IterableLike`, which provides method implementations for an `Iterable` |
| 47 | + - `IndexedSeqOptimized`, which optimises some of these implementations for |
| 48 | + sequences with efficient indexing. |
| 49 | + |
| 50 | +Generally, any trait that is extended recursively is a good candidate to be |
| 51 | +declared a super trait. |
| 52 | + |
| 53 | +### Rules for Inference |
| 54 | + |
| 55 | +Super traits can be given as explicit types as usual. But they are often elided when types are inferred. Roughly, the rules for type inference say that super traits are dropped from intersections where possible. |
| 56 | + |
| 57 | +The precise rules are as follows: |
| 58 | + |
| 59 | + - When inferring a type of a type variable, or the type of a val, or the return type of a def, |
| 60 | + - where that type is not higher-kinded, |
| 61 | + - and where `B` is its known upper bound or `Any` if none exists: |
| 62 | + - If the type inferred so far is of the form `T1 & ... & Tn` where |
| 63 | + `n >= 1`, replace the maximal number of `Ti`s by `Any`, while ensuring that |
| 64 | + the resulting type is still a subtype of the bound `B`. |
| 65 | + - However, do not perform this widening if all types `Ti` can get replaced in that way. |
| 66 | + |
| 67 | +The last clause ensures that a single super trait instance such as `Product` is not widened to `Any`. Super trait instances are only dropped when they appear in conjunction with some other type. |
| 68 | + |
| 69 | +### Syntax |
| 70 | + |
| 71 | +Only the production `TmplDef` for class and trait definitions has to be changed. |
| 72 | +The new version is: |
| 73 | +``` |
| 74 | +TmplDef ::= ([‘case’] ‘class’ | [‘super’] ‘trait’) ClassDef |
| 75 | +``` |
0 commit comments