Skip to content

Commit feabdf0

Browse files
authored
Merge pull request #12312 from som-snytt/issue/10882
Clarify that using val is given member
2 parents 617f127 + b0463a3 commit feabdf0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

docs/docs/reference/contextual/using-clauses.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ inferred argument to `max`. The name of the parameter is left out.
4747

4848
Generally, context parameters may be defined either as a full parameter list `(p_1: T_1, ..., p_n: T_n)` or just as a sequence of types `T_1, ..., T_n`. Vararg parameters are not supported in `using` clauses.
4949

50+
## Class Context Parameters
51+
52+
If a class context parameter is made a member by adding a `val` or `var` modifier,
53+
then that member is available as a given instance.
54+
55+
Compare the following examples, where the attempt to supply an explicit `given` member induces an ambiguity:
56+
57+
```scala
58+
class GivenIntBox(using val givenInt: Int):
59+
def n = summon[Int]
60+
61+
class GivenIntBox2(using givenInt: Int):
62+
given Int = givenInt
63+
//def n = summon[Int] // ambiguous
64+
```
65+
66+
The `given` member is importable as explained in the section on [importing `given`s](./given-imports.md):
67+
68+
```scala
69+
val b = GivenIntBox(using 23)
70+
import b.given
71+
summon[Int] // 23
72+
73+
import b.*
74+
//givenInt // Not found
75+
```
76+
5077
## Inferring Complex Arguments
5178

5279
Here are two other methods that have a context parameter of type `Ord[T]`:

0 commit comments

Comments
 (0)