forked from scala/docs.scala-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scala Tour: call them "implicit conversions" not "views"
- Loading branch information
Showing
10 changed files
with
61 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ _site | |
.DS_Store | ||
.project | ||
.settings | ||
/.jekyll-metadata | ||
*~ | ||
vendor/bundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
layout: tutorial | ||
title: Implicit Conversions | ||
|
||
disqus: true | ||
|
||
tutorial: scala-tour | ||
num: 32 | ||
language: es | ||
--- | ||
|
||
(this page has not been translated into Spanish) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
layout: tutorial | ||
title: Implicit Conversions | ||
|
||
disqus: true | ||
|
||
tutorial: scala-tour | ||
num: 26 | ||
tutorial-next: polymorphic-methods | ||
tutorial-previous: implicit-parameters | ||
--- | ||
|
||
An implicit conversion from type `S` to type `T` is defined by an implicit value which has function type `S => T`, or by an implicit method convertible to a value of that type. | ||
|
||
Implicit conversions are applied in two situations: | ||
|
||
* If an expression `e` is of type `S`, and `S` does not conform to the expression's expected type `T`. | ||
* In a selection `e.m` with `e` of type `T`, if the selector `m` does not denote a member of `T`. | ||
|
||
In the first case, a conversion `c` is searched for which is applicable to `e` and whose result type conforms to `T`. | ||
In the second case, a conversion `c` is searched for which is applicable to `e` and whose result contains a member named `m`. | ||
|
||
The following operation on the two lists xs and ys of type `List[Int]` is legal: | ||
|
||
xs <= ys | ||
|
||
assuming the implicit methods `list2ordered` and `int2ordered` defined below are in scope: | ||
|
||
implicit def list2ordered[A](x: List[A]) | ||
(implicit elem2ordered: A => Ordered[A]): Ordered[List[A]] = | ||
new Ordered[List[A]] { /* .. */ } | ||
|
||
implicit def int2ordered(x: Int): Ordered[Int] = | ||
new Ordered[Int] { /* .. */ } | ||
|
||
The implicitly imported object `scala.Predef` declares several predefined types (e.g. `Pair`) and methods (e.g. `assert`) but also several implicit conversions. | ||
|
||
For example, when calling a Java method that expects a `java.lang.Integer`, you are free to pass it a `scala.Int` instead. That's because Predef includes the following implicit conversions: | ||
|
||
implicit def int2Integer(x: Int) = | ||
java.lang.Integer.valueOf(x) | ||
|
||
To define your own implicit conversions, you must first `import scala.language.implicitConversions` (or invoke the compiler with `-language:implicitConversions`). The feature must be explicitly enabled because it has pitfalls if used indiscriminately. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.