Skip to content
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

Supplement structural givens doc #20327

Merged
merged 1 commit into from
Jul 4, 2024
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
41 changes: 40 additions & 1 deletion docs/_docs/reference/contextual/givens.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,45 @@ given (using config: Config): Factory = MemoizingFactory(config)
An alias given can have type parameters and context parameters just like any other given,
but it can only implement a single type.

## Abstract Givens

A given may be an abstract member, with the restriction that it must have an explicit name.

```scala
trait HasOrd[T]:
given ord: Ord[T]
```

## More Structural Givens

If an alias given instance is analogous to a lazy val,
and a structural given instance is analogous to an object,
albeit an object with an explicit type,
then a structural given may also be specified without an explicit type:

```scala
class IntOrd extends Ord[Int]:
def compare(x: Int, y: Int) =
if x < y then -1 else if x > y then +1 else 0

given IntOrd()
```

Compare this syntax to:

```scala
object intOrd extends IntOrd()
```

The empty parentheses are optional in the extends clause when defining a class,
but are required when defining a given.

Further mixins are allowed as usual:

```scala
given IntOrd() with OrdOps[Int]
```

## Given Macros

Given aliases can have the `inline` and `transparent` modifiers.
Expand Down Expand Up @@ -191,4 +230,4 @@ of given instances:
- A _structural instance_ contains one or more types or constructor applications,
followed by `with` and a template body that contains member definitions of the instance.
- An _alias instance_ contains a type, followed by `=` and a right-hand side expression.
- An _abstract instance_ contains just the type, which is not followed by anything.
- An _abstract instance_ contains just the name and type, which is not followed by anything.