Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class CompletionTest {

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

@Test def completionFromScalaPackageObject: Unit = {
Expand Down
9 changes: 9 additions & 0 deletions library/src/scala/Conversion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ package scala
* Also note that in bootstrapped dotty, `Predef.<:<` should inherit from
* `Conversion`. This would cut the number of special cases in `discardForView`
* from two to one.
*
* The `Conversion` class can also be used to convert explicitly, using
* the `convert` extension method.
*/
@java.lang.FunctionalInterface
abstract class Conversion[-T, +U] extends Function1[T, U]:
/** Convert value `x` of type `T` to type `U` */
def apply(x: T): U
object Conversion:

extension [T](x: T)
/** `x.convert[U]` converts a value `x` of type `T` to type `U` */
def convert[U](using c: Conversion[T, U]) = c(x)
7 changes: 7 additions & 0 deletions tests/pos/convert.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Conversion.convert

given Conversion[String, Int] = _.length
given Conversion[Int, String] = _.toString

def f(x: String): Int = x.convert
def g(x: Int): String = x.convert[String]