You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
12
+
adds as a mixin 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
13
```scala
14
14
traitKind
15
15
caseobjectVarextendsKind
@@ -20,46 +20,35 @@ Here, the inferred type of `x` is `Set[Kind & Product & Serializable]` whereas o
20
20
21
21
- The type of the conditional above is the union type `Val | Var`.
22
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`.
23
+
not a union type. In the example, this type is `Kind & Product & Serializable` since all three traits are traits of both `Val` and `Var`.
24
24
So that type becomes the inferred element type of the set.
25
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`:
26
+
Scala 3 allows one to mark a trait as a `@mixin` 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 mixin trait `S` instead of `Product`:
28
28
```scala
29
-
supertraitS
29
+
@mixintraitS
30
30
traitKind
31
31
objectVarextendsKind, S
32
32
objectValextendsKind, S
33
33
valx=Set(if condition thenValelseVar)
34
34
```
35
-
Now `x` has inferred type `Set[Kind]`. The common super trait `S` does not
35
+
Now `x` has inferred type `Set[Kind]`. The common mixin trait `S` does not
36
36
appear in the inferred type.
37
37
38
-
### Super Traits
38
+
### Mixin Traits
39
39
40
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.
41
+
are treated automatically as mixin traits. Other traits can be turned into mixin traits, by adding the annotation `@mixin` in front of `trait`, as shown above.
42
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
43
+
Every trait can be declared as a mixin trait. Typically mixin 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
44
standard collection library:
45
45
46
46
-`IterableOps`, which provides method implementations for an `Iterable`
47
47
-`StrictOptimizedSeqOps`, which optimises some of these implementations for
48
48
sequences with efficient indexing.
49
49
50
50
Generally, any trait that is extended recursively is a good candidate to be
51
-
declared a super trait.
52
-
53
-
### Retro-Fitting Scala 2 Libraries
54
-
55
-
To allow cross-building between Scala 2 and 3, super traits can also be
56
-
introduced by adding the `@superTrait` annotation, which is defined in package `scala.annotation`. Example:
The `@superTrait` annotation will be deprecated and removed in some later version of Scala when cross-building with Scala 2 will no longer be a concern.
51
+
declared a mixin trait.
63
52
64
53
### Rules for Inference
65
54
@@ -75,12 +64,5 @@ The precise rules are as follows:
75
64
the resulting type is still a subtype of the bound `B`.
76
65
- However, do not perform this widening if all types `Ti` can get replaced in that way.
77
66
78
-
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.
79
-
80
-
### Syntax
67
+
The last clause ensures that a single mixin trait instance such as `Product` is not widened to `Any`. Mixin trait instances are only dropped when they appear in conjunction with some other type.
81
68
82
-
Only the production `TmplDef` for class and trait definitions has to be changed.
0 commit comments