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 {
5858}
5959
6060trait SafeDiv {
61- import IntOpsEx ._
61+ import IntOpsEx ._ // brings safeDiv and safeMod into scope
6262
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
6565 (i.safeDiv(d), i.safeMod(d)) match {
6666 case (Some (d), Some (r)) => Some ((d, r))
6767 case _ => None
@@ -79,19 +79,9 @@ given ops1 as IntOps // brings safeMod into scope
7979
8080Then ` safeMod ` is legal everywhere ` ops1 ` is available. Anonymous givens (and any other form of givens) are supported as well:
8181``` 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)
9283
93- 1 .safeMod(2 )
94- 2 .safeDiv(3 ) // compile error
84+ 1 .divide(2 )
9585```
9686
9787The 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