8000 Add docs · scala/scala3@ac3a2b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac3a2b3

Browse files
committed
Add docs
1 parent eaeff32 commit ac3a2b3

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
---
22
layout: doc-page
3-
title: Mixin Traits
3+
title: Transparent Traits
44
---
55

66
Traits are used in two roles:
77

88
1. As mixins for other classes and traits
99
2. As types of vals, defs, or parameters
1010

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 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:
1312
```scala
1413
trait Kind
1514
case object Var extends Kind
@@ -23,46 +22,46 @@ Here, the inferred type of `x` is `Set[Kind & Product & Serializable]` whereas o
2322
not a union type. In the example, this type is `Kind & Product & Serializable` since all three traits are traits of both `Val` and `Var`.
2423
So that type becomes the inferred element type of the set.
2524

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`:
2826
```scala
29-
@mixin trait S
27+
transparent trait S
3028
trait Kind
3129
object Var extends Kind, S
3230
object Val extends Kind, S
3331
val x = Set(if condition then Val else Var)
3432
```
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
3634
appear in the inferred type.
3735

38-
### Mixin Traits
36+
### Transparent Traits
3937

4038
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.
4241

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:
4544

4645
- `IterableOps`, which provides method implementations for an `Iterable`
4746
- `StrictOptimizedSeqOps`, which optimises some of these implementations for
4847
sequences with efficient indexing.
4948

5049
Generally, any trait that is extended recursively is a good candidate to be
51-
declared a mixin trait.
50+
declared transparent.
5251

5352
### Rules for Inference
5453

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.
5655

5756
The precise rules are as follows:
5857

5958
- When inferring a type of a type variable, or the type of a val, or the return type of a def,
6059
- where that type is not higher-kinded,
6160
- and where `B` is its known upper bound or `Any` if none exists:
6261
- 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
6463
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.
6665

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.
6867

docs/sidebar.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ sidebar:
8989
subsection:
9090
- title: Trait Parameters
9191
url: docs/reference/other-new-features/trait-parameters.html
92-
- title: Mixin Traits
93-
url: docs/reference/other-new-features/mixin-traits.html
92+
- title: Transparent Traits
93+
url: docs/reference/other-new-features/transparent-traits.html
9494
- title: Creator Applications
9595
url: docs/reference/other-new-features/creator-applications.html
9696
- title: Export Clauses

0 commit comments

Comments
 (0)
0