Skip to content

Add code tabs for _overviews/scala3-book/types-intersection.md #2653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion _overviews/scala3-book/types-intersection.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ num: 50
previous-page: types-generics
next-page: types-union
---

<span class="tag tag-inline">Scala 3 only</span>

Used on types, the `&` operator creates a so called _intersection type_.
The type `A & B` represents values that are **both** of the type `A` and of the type `B` at the same time.
For instance, the following example uses the intersection type `Resettable & Growable[String]`:

{% tabs intersection-reset-grow %}

{% tab 'Scala 3 Only' %}

```scala
trait Resettable:
def reset(): Unit
Expand All @@ -25,6 +29,10 @@ def f(x: Resettable & Growable[String]): Unit =
x.add("first")
```

{% endtab %}

{% endtabs %}

In the method `f` in this example, the parameter `x` is required to be *both* a `Resettable` and a `Growable[String]`.

The _members_ of an intersection type `A & B` are all the members of `A` and all the members of `B`.
Expand Down