Skip to content

Commit 02cee1f

Browse files
authored
Merge pull request #11668 from dotty-staging/add-conversion-into
Add Conversion.into method
2 parents a4bee46 + 6fa607b commit 02cee1f

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

language-server/test/dotty/tools/languageserver/CompletionTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CompletionTest {
3030

3131
@Test def completionFromScalaPackage: Unit = {
3232
code"class Foo { val foo: Conv${m1} }".withSource
33-
.completion(m1, Set(("Conversion", Class, "scala.Conversion")))
33+
.completion(m1, Set(("Conversion", Class, "class and object Conversion")))
3434
}
3535

3636
@Test def completionFromScalaPackageObject: Unit = {

library/src/scala/Conversion.scala

+9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ package scala
1717
* Also note that in bootstrapped dotty, `Predef.<:<` should inherit from
1818
* `Conversion`. This would cut the number of special cases in `discardForView`
1919
* from two to one.
20+
*
21+
* The `Conversion` class can also be used to convert explicitly, using
22+
* the `convert` extension method.
2023
*/
2124
@java.lang.FunctionalInterface
2225
abstract class Conversion[-T, +U] extends Function1[T, U]:
26+
/** Convert value `x` of type `T` to type `U` */
2327
def apply(x: T): U
28+
object Conversion:
29+
30+
extension [T](x: T)
31+
/** `x.convert[U]` converts a value `x` of type `T` to type `U` */
32+
def convert[U](using c: Conversion[T, U]) = c(x)

tests/pos/convert.scala

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Conversion.convert
2+
3+
given Conversion[String, Int] = _.length
4+
given Conversion[Int, String] = _.toString
5+
6+
def f(x: String): Int = x.convert
7+
def g(x: Int): String = x.convert[String]

0 commit comments

Comments
 (0)