You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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>
Copy file name to clipboardExpand all lines: _tour/implicit-conversions.md
+62-2Lines changed: 62 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -38,9 +38,69 @@ In the second case, a conversion `c` is searched for, which is applicable to `e`
38
38
39
39
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.)
40
40
41
-
### How are implicit conversions selected?
41
+
### How are implicit conversions brought into scope? ###
42
42
43
-
See this [Scala FAQ Answer](https://docs.scala-lang.org/tutorials/FAQ/index.html#where-does-scala-look-for-implicits).
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
+
importscala.language.implicitConversions// required to define an implicit conversion
valreginald: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 (`*`).
0 commit comments