@@ -4,36 +4,41 @@ title: "Importing Givens"
4
4
---
5
5
6
6
A special form of import wildcard selector is used to import given instances. Example:
7
+
7
8
``` scala
8
9
object A {
9
10
class TC
10
11
given tc as TC
11
12
def f (using TC ) = ???
12
13
}
14
+
13
15
object B {
14
16
import A ._
15
17
import A .{given _ }
16
18
}
17
19
```
20
+
18
21
In the code above, the ` import A._ ` clause of object ` B ` will import all members
19
22
of ` A ` _ except_ the given instance ` tc ` . Conversely, the second import ` import A.{given _} ` will import _ only_ that given instance.
20
23
The two import clauses can also be merged into one:
24
+
21
25
``` scala
22
- object B
26
+ object B {
23
27
import A .{given _ , _ }
28
+ }
24
29
```
25
30
26
31
Generally, a normal wildcard selector ` _ ` brings all definitions other than givens or extensions into scope
27
32
whereas a ` given _ ` selector brings all givens (including those resulting from extensions) into scope.
28
33
29
34
There are two main benefits arising from these rules:
30
35
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.
37
42
38
43
### Importing By Type
39
44
@@ -42,31 +47,40 @@ Since givens can be anonymous it is not always practical to import them by their
42
47
``` scala
43
48
import A .{given TC }
44
49
```
50
+
45
51
This imports any given in ` A ` that has a type which conforms to ` TC ` . Importing givens of several types ` T1,...,Tn `
46
52
is expressed by multiple ` given ` selectors.
47
- ```
53
+
54
+ ``` scala
48
55
import A .{given T1 , ..., given Tn }
49
56
```
57
+
50
58
Importing all given instances of a parameterized type is expressed by wildcard arguments.
51
59
For instance, assuming the object
60
+
52
61
``` scala
53
62
object Instances {
54
63
given intOrd as Ordering [Int ]
55
- given [T : Ordering ] listOrd as Ordering [List [T ]]
64
+ given listOrd [T : Ordering ] as Ordering [List [T ]]
56
65
given ec as ExecutionContext = ...
57
66
given im as Monoid [Int ]
58
67
}
59
68
```
69
+
60
70
the import
71
+
61
72
``` scala
62
73
import Instances .{given Ordering [? ], given ExecutionContext }
63
74
```
75
+
64
76
would import the ` intOrd ` , ` listOrd ` , and ` ec ` instances but leave out the ` im ` instance, since it fits none of the specified bounds.
65
77
66
78
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
+
67
80
``` scala
68
81
import Instances .{im , given Ordering [? ]}
69
82
```
83
+
70
84
would import ` im ` , ` intOrd ` , and ` listOrd ` but leave out ` ec ` .
71
85
72
86
<!--
@@ -115,4 +129,4 @@ ImportSelectors ::= id [‘=>’ id | ‘=>’ ‘_’] [‘,’ ImportSelect
115
129
WildCardSelector ::= ‘_'
116
130
| ‘given’ (‘_' | InfixType)
117
131
Export ::= ‘export’ ImportExpr {‘,’ ImportExpr}
118
- ```
132
+ ```
0 commit comments