You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/givens.md
+15Lines changed: 15 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,6 +90,21 @@ transparent inline given mkAnnotations[A, T] as Annotations[A, T] = ${
90
90
```
91
91
Since `mkAnnotations` is `transparent`, the type of an application is the type of its right hand side, which can be a proper subtype of the declared result type `Annotations[A, T]`.
92
92
93
+
## Pattern-Bound Given Instances
94
+
95
+
Given instances can also appear as pattern bound-variables. Example:
96
+
97
+
```scala
98
+
forgivenContext<- applicationContexts do
99
+
100
+
pair match
101
+
case (ctx asgivenContext, y) => ...
102
+
```
103
+
In the first fragment above, anonymous given instances for class `Context` are established by enumerating over `applicationContexts`. In the second fragment, a given `Context`
104
+
instance named `ctx` is established by matching against the first half of the `pair` selector.
105
+
106
+
In each case, a pattern-bound given instance consists of `given` and a type `T`. The pattern matches exactly the same selectors as the type ascription pattern `_: T`.
107
+
93
108
## Given Instance Initialization
94
109
95
110
A given instance without type or context parameters is initialized on-demand, the first
Copy file name to clipboardExpand all lines: docs/docs/reference/metaprogramming/inline.md
+16-8Lines changed: 16 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -268,10 +268,10 @@ val obj2 = choose(false) // static type is B
268
268
// obj1.m() // compile-time error: `m` is not defined on `A`
269
269
obj2.m() // OK
270
270
```
271
-
Here, the inline method `choose` returns an instance of either of the two types `A` or `B`.
271
+
Here, the inline method `choose` returns an instance of either of the two types `A` or `B`.
272
272
If `choose` had not been declared to be `transparent`, the result
273
-
of its expansion would always be of type `A`, even though the computed value might be of the subtype `B`.
274
-
The inline method is a "blackbox" in the sense that details of its implementation do not leak out.
273
+
of its expansion would always be of type `A`, even though the computed value might be of the subtype `B`.
274
+
The inline method is a "blackbox" in the sense that details of its implementation do not leak out.
275
275
But if a `transparent` modifier is given, the expansion is the type of the expanded body. If the argument `b`
276
276
is `true`, that type is `A`, otherwise it is `B`. Consequently, calling `m` on `obj2`
277
277
type-checks since `obj2` has the same type as the expansion of `choose(false)`, which is `B`.
@@ -571,16 +571,24 @@ would use it as follows:
571
571
importscala.compiletime.summonFrom
572
572
573
573
inlinedefsetFor[T]:Set[T] = summonFrom {
574
-
casegivenord:Ordering[T] =>newTreeSet[T]
575
-
case _ =>newHashSet[T]
574
+
caseord: Ordering[T] =>newTreeSet[T](using ord)
575
+
case _ =>newHashSet[T]
576
576
}
577
577
```
578
578
A `summonFrom` call takes a pattern matching closure as argument. All patterns
579
579
in the closure are type ascriptions of the form `identifier : Type`.
580
580
581
-
Patterns are tried in sequence. The first case with a pattern `x: T` such that
582
-
an implicit value of type `T` can be summoned is chosen. If the pattern is prefixed
583
-
with `given`, the variable `x` is bound to the implicit value for the remainder of the case. It can in turn be used as an implicit in the right hand side of the case. It is an error if one of the tested patterns gives rise to an ambiguous implicit search.
581
+
Patterns are tried in sequence. The first case with a pattern `x: T` such that an implicit value of type `T` can be summoned is chosen.
582
+
583
+
Alternatively, one can also use a pattern-bound given instance, which avoids the explicit using clause. For instance, `setFor` could also be formulated as follows:
584
+
```scala
585
+
importscala.compiletime.summonFrom
586
+
587
+
inlinedefsetFor[T]:Set[T] = summonFrom {
588
+
casegivenOrdering[T] =>newTreeSet[T]
589
+
case _ =>newHashSet[T]
590
+
}
591
+
```
584
592
585
593
`summonFrom` applications must be reduced at compile time.
0 commit comments