Skip to content

Commit 454518f

Browse files
Backport "Supplement structural givens doc" to LTS (#21168)
Backports #20327 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 839359b + 1fb6922 commit 454518f

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

docs/_docs/reference/contextual/givens.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,45 @@ given (using config: Config): Factory = MemoizingFactory(config)
8888
An alias given can have type parameters and context parameters just like any other given,
8989
but it can only implement a single type.
9090

91+
## Abstract Givens
92+
93+
A given may be an abstract member, with the restriction that it must have an explicit name.
94+
95+
```scala
96+
trait HasOrd[T]:
97+
given ord: Ord[T]
98+
```
99+
100+
## More Structural Givens
101+
102+
If an alias given instance is analogous to a lazy val,
103+
and a structural given instance is analogous to an object,
104+
albeit an object with an explicit type,
105+
then a structural given may also be specified without an explicit type:
106+
107+
```scala
108+
class IntOrd extends Ord[Int]:
109+
def compare(x: Int, y: Int) =
110+
if x < y then -1 else if x > y then +1 else 0
111+
112+
given IntOrd()
113+
```
114+
115+
Compare this syntax to:
116+
117+
```scala
118+
object intOrd extends IntOrd()
119+
```
120+
121+
The empty parentheses are optional in the extends clause when defining a class,
122+
but are required when defining a given.
123+
124+
Further mixins are allowed as usual:
125+
126+
```scala
127+
given IntOrd() with OrdOps[Int]
128+
```
129+
91130
## Given Macros
92131

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

0 commit comments

Comments
 (0)