Skip to content

Commit 5455c89

Browse files
committed
Remove motivation from description of summonFrom
1 parent dc14f81 commit 5455c89

File tree

1 file changed

+2
-35
lines changed

1 file changed

+2
-35
lines changed

docs/_docs/reference/metaprogramming/compiletime-ops.md

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -174,40 +174,7 @@ val addition: 1 + 1 = 2
174174

175175
## Summoning Givens Selectively
176176

177-
It is foreseen that many areas of typelevel programming can be done with rewrite
178-
methods instead of implicits. But sometimes implicits are unavoidable. The
179-
problem so far was that the Prolog-like programming style of implicit search
180-
becomes viral: Once some construct depends on implicit search it has to be
181-
written as a logic program itself. Consider for instance the problem of creating
182-
a `TreeSet[T]` or a `HashSet[T]` depending on whether `T` has an `Ordering` or
183-
not. We can create a set of given instances like this:
184-
185-
```scala
186-
trait SetFor[T, S <: Set[T]]
187-
188-
class LowPriority:
189-
given hashSetFor[T]: SetFor[T, HashSet[T]] = ...
190-
191-
object SetsFor extends LowPriority:
192-
given treeSetFor[T: Ordering]: SetFor[T, TreeSet[T]] = ...
193-
```
194-
195-
Clearly, this is not pretty. Besides all the usual indirection of implicit
196-
search, we face the problem of rule prioritization where we have to ensure that
197-
`treeSetFor` takes priority over `hashSetFor` if the element type has an
198-
ordering. This is solved (clumsily) by putting `hashSetFor` in a superclass
199-
`LowPriority` of the object `SetsFor` where `treeSetFor` is defined. Maybe the
200-
boilerplate would still be acceptable if the crufty code could be contained.
201-
However, this is not the case. Every user of the abstraction has to be
202-
parameterized itself with a `SetFor` implicit. Considering the simple task _"I
203-
want a `TreeSet[T]` if `T` has an ordering and a `HashSet[T]` otherwise"_, this
204-
seems like a lot of ceremony.
205-
206-
There are some proposals to improve the situation in specific areas, for
207-
instance by allowing more elaborate schemes to specify priorities. But they all
208-
keep the viral nature of implicit search programs based on logic programming.
209-
210-
By contrast, the new `summonFrom` construct makes implicit search available
177+
The new `summonFrom` construct makes implicit search available
211178
in a functional context. To solve the problem of creating the right set, one
212179
would use it as follows:
213180

@@ -223,7 +190,7 @@ inline def setFor[T]: Set[T] = summonFrom {
223190
A `summonFrom` call takes a pattern matching closure as argument. All patterns
224191
in the closure are type ascriptions of the form `identifier : Type`.
225192

226-
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.
193+
Patterns are tried in sequence. The first case with a pattern `x: T` such that a contextual value of type `T` can be summoned is chosen.
227194

228195
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:
229196

0 commit comments

Comments
 (0)