You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/import-delegate.md
+19-21Lines changed: 19 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -3,12 +3,12 @@ layout: doc-page
3
3
title: "Import Given"
4
4
---
5
5
6
-
A special form of import selector is used to import given instances. Example:
6
+
A special form of import wildcard selector is used to import given instances. Example:
7
7
```scala
8
8
objectA {
9
9
classTC
10
-
giventc asTC
11
-
deff where TC=???
10
+
giventc:TC
11
+
deff(givenTC)=???
12
12
}
13
13
objectB {
14
14
importA._
@@ -19,16 +19,16 @@ In the code above, the `import A._` clause of object `B` will import all members
19
19
of `A`_except_ the given instance `tc`. Conversely, the second import `import A.given` will import _only_ that given instance.
20
20
The two import clauses can also be merged into one:
21
21
```scala
22
-
objectB:
22
+
objectB
23
23
importA.{given, _}
24
24
```
25
25
26
-
Generally, a normal import selector brings definitions other than given instances into scope whereas a `given`import selector brings only given instances into scope.
26
+
Generally, a normal wildcard selector `_`brings all definitions other than given instances into scope whereas a `given` selector brings all given instances into scope.
27
27
28
28
There are two main benefits arising from these rules:
29
29
30
30
- It is made clearer where givens in scope are coming from.
31
-
In particular, it is not possible to hide imported givens in a long list of regular imports.
31
+
In particular, it is not possible to hide imported givens in a long list of regular wildcard imports.
32
32
- It enables importing all givens
33
33
without importing anything else. This is particularly important since givens
34
34
can be anonymous, so the usual recourse of using named imports is not
@@ -39,32 +39,32 @@ There are two main benefits arising from these rules:
39
39
Since givens can be anonymous it is not always practical to import them by their name, and wildcard imports are typically used instead. By-type imports provide a more specific alternative to wildcard imports, which makes it clearer what is imported. Example:
40
40
41
41
```scala
42
-
importA.{givenasTC}
42
+
importA.{givenTC}
43
43
```
44
44
This imports any given in `A` that has a type which conforms to `TC`. Importing givens of several types `T1,...,Tn`
45
45
is expressed by multiple `given` selectors.
46
46
```
47
-
import A.{given as T1, ..., given as Tn}
47
+
import A.{given T1, ..., given Tn}
48
48
```
49
49
Importing all given instances of a parameterized type is expressed by wildcard arguments.
@@ -50,14 +50,14 @@ Given instances can be mapped to combinations of implicit objects, classes and i
50
50
51
51
Anonymousgiveninstances get compiler synthesized names, which are generated in a reproducible way from the implemented type(s). For example, if the names of the `IntOrd` and `ListOrd` givens above were left out, the following names would be synthesized instead:
52
52
```scala
53
-
givenOrd_Int_given asOrd[Int] { ... }
54
-
givenOrd_List_given[T] asOrd[List[T]] { ... }
53
+
givengiven_Ord_Int:Ord[Int] { ... }
54
+
givengiven_Ord_List[T] :Ord[List[T]] { ... }
55
55
```
56
56
The synthesized type names are formed from
57
57
58
+
- the prefix `given_`,
58
59
- the simple name(s) of the implemented type(s), leaving out any prefixes,
59
-
- the simple name(s) of the toplevel argument type constructors to these types
60
-
- the suffix `_given`.
60
+
- the simple name(s) of the toplevel argument type constructors to these types.
61
61
62
62
Anonymous given instances that define extension methods without also implementing a type
63
63
get their name from the name of the first extension method and the toplevel type
@@ -67,7 +67,7 @@ constructor of its first parameter. For example, the given instance
67
67
def (xs: List[T]) second[T] = ...
68
68
}
69
69
```
70
-
gets the synthesized name `second_of_List_T_given`.
70
+
gets the synthesized name `given_second_of_List_T`.
71
71
72
72
### Given Clauses
73
73
@@ -134,7 +134,7 @@ Implicit conversion methods in Scala 2 can be expressed as given instances of th
134
134
```
135
135
one can write
136
136
```scala
137
-
givenstringToToken asConversion[String, Token] {
137
+
givenstringToToken:Conversion[String, Token] {
138
138
defapply(str: String):Token=newKeyWord(str)
139
139
}
140
140
```
@@ -153,7 +153,7 @@ E.g., Scala 2's
153
153
can be expressed in Dotty as
154
154
```scala
155
155
lazyvalpos:Position= tree.sourcePos
156
-
givenasPosition= pos
156
+
givenPosition= pos
157
157
```
158
158
159
159
### Abstract Implicits
@@ -165,7 +165,7 @@ An abstract implicit `val` or `def` in Scala 2 can be expressed in Dotty using a
0 commit comments