Skip to content

Commit

Permalink
Backport "Supplement structural givens doc" to LTS (#21168)
Browse files Browse the repository at this point in the history
Backports #20327 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jul 11, 2024
2 parents 839359b + 1fb6922 commit 454518f
Showing 1 changed file with 40 additions and 1 deletion.
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.

0 comments on commit 454518f

Please sign in to comment.