Skip to content

Commit 35ec471

Browse files
Update implicit-conversions.md (#2674)
* Update implicit-conversions.md I'm not sure if it is intentional, but the Tour-of-Scala documentation does not mention how to bring implicit conversions into scope. In fact, that information is a bit hard to find anywhere else, so I thought I'll propose to add it here? * Update _tour/implicit-conversions.md from suggestion Co-authored-by: Jamie Thompson <bishbashboshjt@gmail.com> * Update implicit-conversions.md Improved Scala 3 description. Thanks for all the suggestions! * Fix import in _tour/implicit-conversions.md Co-authored-by: Jamie Thompson <bishbashboshjt@gmail.com> * describe Scala 2 implicit conversion in companion object * Remove mdoc from Scala 3 example * Update _tour/implicit-conversions.md * Update _tour/implicit-conversions.md * Remove mdoc from Scala 2 example as well Sorry for wasting your time, next time, I'll install the toolchain. * merge section "How are implicit conversions selected?" Co-authored-by: Jamie Thompson <bishbashboshjt@gmail.com>
1 parent 2f1eba7 commit 35ec471

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

_tour/implicit-conversions.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,69 @@ In the second case, a conversion `c` is searched for, which is applicable to `e`
3838

3939
An example is to compare two strings `"foo" < "bar"`. In this case, `String` has no member `<`, so the implicit conversion `Predef.augmentString("foo") < "bar"` is inserted. (`scala.Predef` is automatically imported into all Scala programs.)
4040

41-
### How are implicit conversions selected?
41+
### How are implicit conversions brought into scope? ###
4242

43-
See this [Scala FAQ Answer](https://docs.scala-lang.org/tutorials/FAQ/index.html#where-does-scala-look-for-implicits).
43+
{% tabs implicit-conversion-scope class=tabs-scala-version %}
44+
{% tab 'Scala 2' %}
45+
In Scala 2, an implicit conversion is brought into scope by importing from the object that defined it, (e.g. `Conversions` in this case). If the implicit conversion is in the companion object of the argument type, (e.g. `Student` in this case), then no import is necessary.
46+
47+
```scala
48+
import scala.language.implicitConversions // required to define an implicit conversion
49+
50+
case class Student(name: String)
51+
object Student {
52+
implicit def fromStudentToInt(student: Student): Int = student.name.length
53+
}
54+
55+
object Conversions {
56+
implicit def fromStringToStudent(name: String): Student = Student(name)
57+
}
58+
59+
import Conversions._
60+
object Usage {
61+
def main(args: Array[String]) = {
62+
val reginald: Student = "Reginald" // applies the conversion Conversions.fromStringToStudent
63+
println(reginald + 2) // applies the conversion Student.fromStudentToInt
64+
}
65+
}
66+
```
67+
{% endtab %}
68+
{% tab 'Scala 3' %}
69+
In Scala 3, an implicit conversion is brought into scope by either importing `given` or the named conversion from the object that defined it, (e.g. `Conversions` in this case).
70+
71+
Note that as of Scala 3, implicit conversions cannot be brought into scope anymore by means of a wildcard import (`*`).
72+
73+
Given the example:
74+
75+
```scala
76+
case class Student(name: String):
77+
def printName: Unit = println(name)
78+
object Student:
79+
given Conversion[Student, Int] = _.name.length
80+
81+
object Conversions:
82+
given fromStringToStudent: Conversion[String, Student] = Student(_)
83+
```
84+
85+
The following imports would bring the `Conversion[String, Student]` into scope:
86+
- `import Conversions.given`
87+
- `import Conversions.{given Conversion[String, Student]}`
88+
- `import Conversions.fromStringToStudent`
89+
90+
If the implicit conversion is in the companion object of the argument type, (e.g. `Student` in this case), then no import is necessary.
91+
92+
```scala
93+
import Conversions.given
94+
object Usage:
95+
@main def run =
96+
val reginald: Student = "Reginald" // applies the Conversion[String, Student]
97+
println(reginald + 2) // applies the Conversion[Student, Int]
98+
```
99+
{% endtab %}
100+
{% endtabs %}
101+
102+
Further reading:
103+
- [Where does Scala look for implicits? (on StackOverflow)](https://docs.scala-lang.org/tutorials/FAQ/index.html#where-does-scala-look-for-implicits).
44104

45105
### Beware the power of implicit conversions
46106

0 commit comments

Comments
 (0)