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 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:
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 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
12
```scala
14
13
traitKind
15
14
caseobjectVarextendsKind
@@ -23,46 +22,46 @@ Here, the inferred type of `x` is `Set[Kind & Product & Serializable]` whereas o
23
22
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
23
So that type becomes the inferred element type of the set.
25
24
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`:
25
+
Scala 3 allows one to mark a mixin trait as `transparent`, which means that it can be suppressed in type inference. Here's an example that follows the lines of the code above, but now with a new transparent trait `S` instead of `Product`:
28
26
```scala
29
-
@mixintraitS
27
+
transparenttraitS
30
28
traitKind
31
29
objectVarextendsKind, S
32
30
objectValextendsKind, S
33
31
valx=Set(if condition thenValelseVar)
34
32
```
35
-
Now `x` has inferred type `Set[Kind]`. The common mixin trait `S` does not
33
+
Now `x` has inferred type `Set[Kind]`. The common transparent trait `S` does not
36
34
appear in the inferred type.
37
35
38
-
### Mixin Traits
36
+
### Transparent Traits
39
37
40
38
The traits `scala.Product`, `java.lang.Serializable` and `java.lang.Comparable`
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.
39
+
are treated automatically as transparent. Other traits can be turned into transparent traits,
40
+
by adding the modifier `transparent` in front of `trait`, as shown above.
42
41
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
-
standard collection library:
42
+
Every trait can be declared as transparent. Typically, transparent traits are traits
43
+
that influence the implementation of inheriting classes and traits and that are not usually used as types by themselves. Two examples from the standard collection library:
45
44
46
45
-`IterableOps`, which provides method implementations for an `Iterable`
47
46
-`StrictOptimizedSeqOps`, which optimises some of these implementations for
48
47
sequences with efficient indexing.
49
48
50
49
Generally, any trait that is extended recursively is a good candidate to be
51
-
declared a mixin trait.
50
+
declared transparent.
52
51
53
52
### Rules for Inference
54
53
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.
54
+
Transparent 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 transparent traits are dropped from intersections where possible.
56
55
57
56
The precise rules are as follows:
58
57
59
58
- When inferring a type of a type variable, or the type of a val, or the return type of a def,
60
59
- where that type is not higher-kinded,
61
60
- and where `B` is its known upper bound or `Any` if none exists:
62
61
- 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
62
+
`n >= 1`, replace the maximal number of transparent `Ti`s by `Any`, while ensuring that
64
63
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.
64
+
- However, do not perform this widening if all transparent traits`Ti` can get replaced in that way.
66
65
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.
66
+
The last clause ensures that a single transparent trait instance such as `Product` is not widened to `Any`. Transparent trait instances are only dropped when they appear in conjunction with some other type.
0 commit comments