@@ -4,36 +4,41 @@ title: "Importing Givens"
44---
55
66A special form of import wildcard selector is used to import given instances. Example:
7+
78``` scala
89object A {
910 class TC
1011 given tc as TC
1112 def f (using TC ) = ???
1213}
14+
1315object B {
1416 import A ._
1517 import A .{given _ }
1618}
1719```
20+
1821In the code above, the ` import A._ ` clause of object ` B ` will import all members
1922of ` A ` _ except_ the given instance ` tc ` . Conversely, the second import ` import A.{given _} ` will import _ only_ that given instance.
2023The 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
2631Generally, a normal wildcard selector ` _ ` brings all definitions other than givens or extensions into scope
2732whereas a ` given _ ` selector brings all givens (including those resulting from extensions) into scope.
2833
2934There 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
4348import A .{given TC }
4449```
50+
4551This imports any given in ` A ` that has a type which conforms to ` TC ` . Importing givens of several types ` T1,...,Tn `
4652is expressed by multiple ` given ` selectors.
47- ```
53+
54+ ``` scala
4855import A .{given T1 , ..., given Tn }
4956```
57+
5058Importing all given instances of a parameterized type is expressed by wildcard arguments.
5159For instance, assuming the object
60+
5261``` scala
5362object 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+
6070the import
71+
6172``` scala
6273import Instances .{given Ordering [? ], given ExecutionContext }
6374```
75+
6476would import the ` intOrd ` , ` listOrd ` , and ` ec ` instances but leave out the ` im ` instance, since it fits none of the specified bounds.
6577
6678By-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
6881import Instances .{im , given Ordering [? ]}
6982```
83+
7084would import ` im ` , ` intOrd ` , and ` listOrd ` but leave out ` ec ` .
7185
7286<!--
@@ -115,4 +129,4 @@ ImportSelectors ::= id [‘=>’ id | ‘=>’ ‘_’] [‘,’ ImportSelect
115129WildCardSelector ::= ‘_'
116130 | ‘given’ (‘_' | InfixType)
117131Export ::= ‘export’ ImportExpr {‘,’ ImportExpr}
118- ```
132+ ```
0 commit comments