Skip to content

Commit c14b942

Browse files
committed
implicits: refactor implicits
1 parent 3847a8c commit c14b942

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/pages/type-classes/implicits.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Working with Given Instances and Using Clauses
1+
## Working with Contextual Abstractions
22

33
```scala mdoc:invisible
44
// Forward definitions
@@ -36,7 +36,7 @@ Working with type classes in Scala means
3636
working with given instances and using clauses.
3737
There are a few rules we need to know to do this effectively.
3838

39-
### Implicit Scope
39+
### Contextual Abstractions Scope
4040

4141
As we saw above, the compiler searches
4242
for candidate type class instances by type.
@@ -49,10 +49,10 @@ Json.toJson("A string!")
4949
```
5050

5151
The places where the compiler searches for candidate instances
52-
is known as the *implicit scope*.
53-
The implicit scope applies at the call site;
52+
is known as the *contextual abstractions scope*.
53+
The contextual abstractions scope applies at the call site;
5454
that is the point where we call a method with a using clause.
55-
The implicit scope which roughly consists of:
55+
The contextual abstractions scope which roughly consists of:
5656

5757
- local or inherited definitions;
5858

@@ -101,14 +101,14 @@ For our purposes, we can package type class instances in roughly five ways:
101101

102102
With option 1 and 2 we bring given instances into scope by `importing` them explicitly.
103103
With option 3 we bring them into scope with inheritance.
104-
With options 4 and 5 instances are *always* in implicit scope,
104+
With options 4 and 5 instances are *always* in the contextual abstractions scope,
105105
regardless of where we try to use them.
106106

107107
It is conventional to put type class instances in a companion object (option 4 and 5 above)
108108
if there is only one sensible implementation,
109109
or at least one implementation that is widely accepted as the default.
110110
This makes type class instances easier to use
111-
as no import is required to bring them into the implicit scope.
111+
as no import is required to bring them into the contextual abstractions scope.
112112

113113
[^implicit-search]: If you're interested in the finer rules of implicit resolution in Scala,
114114
start by taking a look at [this Stack Overflow post on implicit scope][link-so-implicit-scope]
@@ -203,7 +203,9 @@ a combination that creates a type class instance
203203
of the correct overall type.
204204

205205
<div class="callout callout-warning">
206-
*Implicit Conversions*
206+
*Contextual Implicit Conversions*
207+
208+
// TODO: https://docs.scala-lang.org/scala3/reference/contextual/conversions.html
207209

208210
When you create a type class instance constructor
209211
using an `given`,
@@ -230,11 +232,6 @@ trait JsonWriter[A]:
230232
```scala modc:warn
231233
given optionWriter[A](writer: JsonWriter[A]): JsonWriter[Option[A]] =
232234
???
233-
// warning: implicit conversion method foo should be enabled
234-
// by making the implicit value scala.language.implicitConversions visible.
235-
// This can be achieved by adding the import clause 'import scala.language.implicitConversions'
236-
// or by setting the compiler option -language:implicitConversions.
237-
// See the Scaladoc for value scala.language.implicitConversions for a discussion
238-
// why the feature should be explicitly enabled.
235+
// TODO: Fix formatting
239236
```
240237
</div>

0 commit comments

Comments
 (0)