File tree Expand file tree Collapse file tree 1 file changed +5
-15
lines changed
docs/docs/reference/contextual Expand file tree Collapse file tree 1 file changed +5
-15
lines changed Original file line number Diff line number Diff line change @@ -58,10 +58,10 @@ object IntOpsEx extends IntOps {
58
58
}
59
59
60
60
trait SafeDiv {
61
- import IntOpsEx ._
61
+ import IntOpsEx ._ // brings safeDiv and safeMod into scope
62
62
63
- def divide (i : Int , d : Int ) : Option [(Int , Int )] =
64
- // extension method imported and thus in scope
63
+ def (i : Int ) divide( d : Int ) : Option [(Int , Int )] =
64
+ // extension methods imported and thus in scope
65
65
(i.safeDiv(d), i.safeMod(d)) match {
66
66
case (Some (d), Some (r)) => Some ((d, r))
67
67
case _ => None
@@ -79,19 +79,9 @@ given ops1 as IntOps // brings safeMod into scope
79
79
80
80
Then ` safeMod ` is legal everywhere ` ops1 ` is available. Anonymous givens (and any other form of givens) are supported as well:
81
81
``` scala
82
- given SafeDiv // brings safeMod, safeDiv and divide into scope
83
- ```
84
-
85
- In case the extension method is defined in an object as in ` IntOpsEx ` , then it can also be brought into scope by a given:
86
- ``` scala
87
- import given IntOpsEx .type = IntOpsEx // unusual, brings safeMod and safeDiv into scope
88
- ```
89
- However importing from an object is most likely the better choice. And also superior regarding selectivety:
90
- ``` scala
91
- import IntOpsEx .safeMod // brings only safeMod into scope
82
+ given SafeDiv // brings divide into scope (safeMod and safeDiv are not automatically exported)
92
83
93
- 1 .safeMod(2 )
94
- 2 .safeDiv(3 ) // compile error
84
+ 1 .divide(2 )
95
85
```
96
86
97
87
The precise rules for resolving a selection to an extension method are as follows.
You can’t perform that action at this time.
0 commit comments