Skip to content

Commit 68e2c9f

Browse files
committed
Show and test implicit conversions using SAM types.
1 parent 4823308 commit 68e2c9f

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

docs/docs/reference/contextual/conversions.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ implied for Conversion[String, Token] {
1414
def apply(str: String): Token = new KeyWord(str)
1515
}
1616
```
17+
Using an implied alias this can be expressed more concisely as:
18+
```scala
19+
implied for Conversion[String, Token] = new KeyWord(_)
20+
```
1721
An implicit conversion is applied automatically by the compiler in three situations:
1822

1923
1. If an expression `e` has type `T`, and `T` does not conform to the expression's expected type `S`.
@@ -33,9 +37,8 @@ If such an instance `C` is found, the expression `e` is replaced by `C.apply(e)`
3337
primitive number types to subclasses of `java.lang.Number`. For instance, the
3438
conversion from `Int` to `java.lang.Integer` can be defined as follows:
3539
```scala
36-
implied int2Integer for Conversion[Int, java.lang.Integer] {
37-
def apply(x: Int) = new java.lang.Integer(x)
38-
}
40+
implied int2Integer for Conversion[Int, java.lang.Integer] =
41+
new java.lang.Integer(x)
3942
```
4043

4144
2. The "magnet" pattern is sometimes used to express many variants of a method. Instead of defining overloaded versions of the method, one can also let the method take one or more arguments of specially defined "magnet" types, into which various argument types can be converted. E.g.
@@ -56,15 +59,9 @@ object Completions {
5659
//
5760
// CompletionArg.fromStatusCode(statusCode)
5861

59-
implied fromString for Conversion[String, CompletionArg] {
60-
def apply(s: String) = CompletionArg.Error(s)
61-
}
62-
implied fromFuture for Conversion[Future[HttpResponse], CompletionArg] {
63-
def apply(f: Future[HttpResponse]) = CompletionArg.Response(f)
64-
}
65-
implied fromStatusCode for Conversion[Future[StatusCode], CompletionArg] {
66-
def apply(code: Future[StatusCode]) = CompletionArg.Status(code)
67-
}
62+
implied fromString for Conversion[String, CompletionArg] = Error(_)
63+
implied fromFuture for Conversion[Future[HttpResponse], CompletionArg] = Response(_)
64+
implied fromStatusCode for Conversion[Future[StatusCode], CompletionArg] = Status(_)
6865
}
6966
import CompletionArg._
7067

tests/pos/reference/instances.scala

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,9 @@ object Completions {
274274
//
275275
// CompletionArg.from(statusCode)
276276

277-
implied fromString for Conversion[String, CompletionArg] {
278-
def apply(s: String) = CompletionArg.Error(s)
279-
}
280-
implied fromFuture for Conversion[Future[HttpResponse], CompletionArg] {
281-
def apply(f: Future[HttpResponse]) = CompletionArg.Response(f)
282-
}
283-
implied fromStatusCode for Conversion[Future[StatusCode], CompletionArg] {
284-
def apply(code: Future[StatusCode]) = CompletionArg.Status(code)
285-
}
277+
implied fromString for Conversion[String, CompletionArg] = Error(_)
278+
implied fromFuture for Conversion[Future[HttpResponse], CompletionArg] = Response(_)
279+
implied fromStatusCode for Conversion[Future[StatusCode], CompletionArg] = Status(_)
286280
}
287281
import CompletionArg._
288282

0 commit comments

Comments
 (0)