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/using-clauses.md
+18-11Lines changed: 18 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -50,29 +50,36 @@ Generally, context parameters may be defined either as a full parameter list `(p
50
50
51
51
## Class Context Parameters
52
52
53
-
If a class context parameter is made a member by adding a `val` or `var` modifier,
54
-
then that member is available as a given instance.
53
+
To make a class context parameter visible from outside the class body, it can be made into a member by adding a `val` or `var` modifier.
54
+
```scala
55
+
classGivenIntBox(usingvalusingParameter:Int):
56
+
defmyInt= summon[Int]
55
57
56
-
Compare the following examples, where the attempt to supply an explicit `given` member induces an ambiguity:
58
+
valb=GivenIntBox(using23)
59
+
importb.usingParameter
60
+
summon[Int] // 23
61
+
```
57
62
63
+
This is preferable to creating an explicit `given` member, as the latter creates ambiguity inside the class body:
58
64
```scala
59
-
classGivenIntBox(usingvalgivenInt:Int):
60
-
defn= summon[Int]
61
-
62
-
classGivenIntBox2(usinggivenInt: Int):
63
-
givenInt= givenInt
64
-
//def n = summon[Int] // ambiguous
65
+
classGivenIntBox2(usingusingParameter: Int):
66
+
givengivenMember:Int= usingParameter
67
+
defn= summon[Int] // ambiguous given instances: both usingParameter and givenMember match type Int
65
68
```
66
69
67
-
The `given` member is importable as explained in the section on [importing `given`s](./given-imports.md):
70
+
From the outside of `GivenIntBox`, `usingParameter` appears as if it were defined in the class as `given usingParameter: Int`, in particular it must be imported as described in the section on [importing `given`s](./given-imports.md).
0 commit comments