Skip to content

Clarify that using val is given member #12312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/docs/reference/contextual/using-clauses.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ inferred argument to `max`. The name of the parameter is left out.

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.

## Class Context Parameters

If a class context parameter is made a member by adding a `val` or `var` modifier,
then that member is available as a given instance.

Compare the following examples, where the attempt to supply an explicit `given` member induces an ambiguity:

```scala
class GivenIntBox(using val givenInt: Int):
def n = summon[Int]

class GivenIntBox2(using givenInt: Int):
given Int = givenInt
//def n = summon[Int] // ambiguous
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation! Maybe we can use given Int = 42 here? Otherwise one might argue that semantically there is no ambiguity -- only the compiler complains about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting point. My idea was that if you want to "export givenInt" as a given, you might try it this way.

```

The `given` member is importable as explained in the section on [importing `given`s](./given-imports.md):

```scala
val b = GivenIntBox(using 23)
import b.given
summon[Int] // 23

import b.*
//givenInt // Not found
```

## Inferring Complex Arguments

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