Skip to content

Commit 608baf4

Browse files
Merge pull request #9349 from ShapelessCat/fix-code-in-import-givens-doc
Fix example code in given-imports.md and adjust markdown format
2 parents 3456e85 + d25c187 commit 608baf4

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

docs/docs/reference/contextual/given-imports.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,41 @@ title: "Importing Givens"
44
---
55

66
A special form of import wildcard selector is used to import given instances. Example:
7+
78
```scala
89
object A {
910
class TC
1011
given tc as TC
1112
def f(using TC) = ???
1213
}
14+
1315
object B {
1416
import A._
1517
import A.{given _}
1618
}
1719
```
20+
1821
In the code above, the `import A._` clause of object `B` will import all members
1922
of `A` _except_ the given instance `tc`. Conversely, the second import `import A.{given _}` will import _only_ that given instance.
2023
The two import clauses can also be merged into one:
24+
2125
```scala
22-
object B
26+
object B {
2327
import A.{given _, _}
28+
}
2429
```
2530

2631
Generally, a normal wildcard selector `_` brings all definitions other than givens or extensions into scope
2732
whereas a `given _` selector brings all givens (including those resulting from extensions) into scope.
2833

2934
There are two main benefits arising from these rules:
3035

31-
- It is made clearer where givens in scope are coming from.
32-
In particular, it is not possible to hide imported givens in a long list of regular wildcard imports.
33-
- It enables importing all givens
34-
without importing anything else. This is particularly important since givens
35-
can be anonymous, so the usual recourse of using named imports is not
36-
practical.
36+
- It is made clearer where givens in scope are coming from.
37+
In particular, it is not possible to hide imported givens in a long list of regular wildcard imports.
38+
- It enables importing all givens
39+
without importing anything else. This is particularly important since givens
40+
can be anonymous, so the usual recourse of using named imports is not
41+
practical.
3742

3843
### Importing By Type
3944

@@ -42,31 +47,40 @@ Since givens can be anonymous it is not always practical to import them by their
4247
```scala
4348
import A.{given TC}
4449
```
50+
4551
This imports any given in `A` that has a type which conforms to `TC`. Importing givens of several types `T1,...,Tn`
4652
is expressed by multiple `given` selectors.
47-
```
53+
54+
```scala
4855
import A.{given T1, ..., given Tn}
4956
```
57+
5058
Importing all given instances of a parameterized type is expressed by wildcard arguments.
5159
For instance, assuming the object
60+
5261
```scala
5362
object Instances {
5463
given intOrd as Ordering[Int]
55-
given [T: Ordering] listOrd as Ordering[List[T]]
64+
given listOrd[T: Ordering] as Ordering[List[T]]
5665
given ec as ExecutionContext = ...
5766
given im as Monoid[Int]
5867
}
5968
```
69+
6070
the import
71+
6172
```scala
6273
import Instances.{given Ordering[?], given ExecutionContext}
6374
```
75+
6476
would import the `intOrd`, `listOrd`, and `ec` instances but leave out the `im` instance, since it fits none of the specified bounds.
6577

6678
By-type imports can be mixed with by-name imports. If both are present in an import clause, by-type imports come last. For instance, the import clause
79+
6780
```scala
6881
import Instances.{im, given Ordering[?]}
6982
```
83+
7084
would import `im`, `intOrd`, and `listOrd` but leave out `ec`.
7185

7286
<!--
@@ -115,4 +129,4 @@ ImportSelectors ::= id [‘=>’ id | ‘=>’ ‘_’] [‘,’ ImportSelect
115129
WildCardSelector ::= ‘_'
116130
| ‘given’ (‘_' | InfixType)
117131
Export ::= ‘export’ ImportExpr {‘,’ ImportExpr}
118-
```
132+
```

0 commit comments

Comments
 (0)