Skip to content

Commit d7f96c7

Browse files
committed
Add test code
and fix doc accordingly.
1 parent 1eeffc0 commit d7f96c7

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

docs/docs/reference/implicit-by-name-parameters.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,33 @@
33
Call-by-name implicit parameters can be used to avoid a divergent implicit expansion.
44

55
```scala
6+
trait Serializable[T] {
7+
def write(x: T): Unit
8+
}
9+
10+
implicit def serializeInt: Serializable[Int] = ???
11+
612
implicit def serializeOption[T](implicit ev: => Serializable[T]): Serializable[Option[T]] =
713
new Serializable[Option[T]] {
8-
def write(x: Option[T], out: DataOutputStream) = t match {
14+
def write(xo: Option[T]) = xo match {
915
case Some(x) => ev.write(x)
1016
case None =>
1117
}
1218
}
1319

14-
val s = serializeOption[Option[Int]]
15-
s.write(Some(33))
16-
s.write(None)
20+
val s = implicitly[Serializable[Option[Int]]]
21+
22+
s.write(Some(33))
23+
s.write(None)
1724
```
1825
As is the case for a normal by-name parameter, the argument for the implicit parameter `ev`
1926
is evaluated on demand. In the example above, if the option value `x` is `None`, it is
2027
not evaluated at all.
2128

2229
The synthesized argument for an implicit parameter is backed by a lazy
23-
val, which means that the parameter is evaluated at most once. The
24-
lazy val is also available as a value for implicit search, which can
25-
be useful to avoid an otherwise diverging expansion.
30+
val if this is necessary to prevent an otherwise diverging expansion.
2631

27-
The precise steps for constructing an implicit argument for a by-name parameter of type `=> T` are:
32+
The precise steps for constructing an implicit argument for a by-name parameter of type `=> T` are as follows.
2833

2934
1. Create a new implicit value with a fresh name _lv_, which has the signature of the following definition:
3035

@@ -40,3 +45,10 @@ The precise steps for constructing an implicit argument for a by-name parameter
4045

4146
Otherwise, return `E` unchanged.
4247

48+
In the example above, the definition of `s` would be expanded as follows.
49+
50+
val s = implicitly[Test.Serializable[Option[Int]]](
51+
serializeOption[Int](serializeInt)
52+
)
53+
54+
No lazy val was generated because the synthesized argument is not recursive.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
object Test {
2+
3+
trait Serializable[T] {
4+
def write(x: T): Unit
5+
}
6+
7+
implicit def serializeInt: Serializable[Int] = ???
8+
9+
implicit def serializeOption[T](implicit ev: => Serializable[T]): Serializable[Option[T]] =
10+
new Serializable[Option[T]] {
11+
def write(xo: Option[T]) = xo match {
12+
case Some(x) => ev.write(x)
13+
case None =>
14+
}
15+
}
16+
17+
val s = implicitly[Serializable[Option[Int]]]
18+
19+
s.write(Some(33))
20+
s.write(None)
21+
22+
}

0 commit comments

Comments
 (0)