Skip to content

Commit c8ad8ec

Browse files
committed
Add docs
Explain by-type imports on the doc page. Also, add a test for wildcards in bounds.
1 parent 0e401ca commit c8ad8ec

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

docs/docs/reference/contextual/import-implied.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ A special form of import is used to import implied instances. Example:
88
object A {
99
class TC
1010
implied tc for TC
11+
implied
1112
def f given TC = ???
1213
}
1314
object B {
@@ -28,6 +29,35 @@ There are two main benefits arising from these rules:
2829
instances can be anonymous, so the usual recourse of using named imports is not
2930
practical.
3031

32+
### Importing By Type
33+
34+
Since implied instances 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:
35+
36+
```scala
37+
import implied A.{for TC}
38+
```
39+
This imports any implied instance in `A` that has a type which conforms tp `TC`. There can be several bounding types following a `for` and bounding types can contain wildcards.
40+
For instance, assuming the object
41+
```scala
42+
object Instances {
43+
implied intOrd for Ordering[Int]
44+
implied [T: Ordering] listOrd for Ordering[List[T]]
45+
implied ec for ExecutionContext = ...
46+
implied im for Monoid[Int]
47+
}
48+
```
49+
the import
50+
```
51+
import implied Instances.{for Ordering[_], ExecutionContext}
52+
```
53+
would import the `intOrd`, `listOrd`, and `ec` instances but leave out the `im` instance, since it fits none of the specified bounds.
54+
55+
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
56+
```
57+
import implied Instances.{im, for Ordering[_]}
58+
```
59+
would import `im`, `intOrd`, and `listOrd` but leave out `ec`. By-type imports cannot be mixed with a wildcard import in the same import clause.
60+
3161
### Migration
3262

3363
The rules for `import implied` above have the consequence that a library

tests/run/implied-for.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ object A {
44

55
class B extends T
66
class C extends T
7-
class D
7+
class D[T]
88

99
implied b for B
1010
implied c for C
1111
implied t for T
12-
implied d for D
12+
implied d for D[Int]
1313
}
1414

1515
object Test extends App {
1616
import A._
17-
import implied A.{t, for B, D}
17+
import implied A.{t, for B, D[_]}
1818

1919
val x1: B = b
2020
val x2: T = t
21-
val x3: D = d
21+
val x3: D[Int] = d
2222

2323
assert(the[T].isInstanceOf[B])
24-
assert(the[D].isInstanceOf[D])
24+
assert(the[D[Int]].isInstanceOf[D[_]])
2525
}

0 commit comments

Comments
 (0)