An implicit conversion function example in Scala/Dotty

An implicit conversion in Scala lets you provide a way to almost-magically convert one data type to another, such as providing a way to convert a Scala String to an Int.

Here’s a quick example of how to write an implicit conversion function in Scala 3:

// Scala 3: define a conversion from String to Int
given Conversion[String, Int] with
    def apply(s: String): Int = Integer.parseInt(s)

You can also use this more-concise approach:

// Scala 3: can also do this:
given Conversion[String, Int] = Integer.parseInt(_)

With either of those implementations in scope, you can then write code like this and get an automatic conversion from a Scala String to a Scala Int:

// this import seems to still be needed
import scala.language.implicitConversions

// define a method that takes an Int
def plus1(i: Int) = i + 1

// give it a String instead of an Int
plus1("1")

I just tested that code with the Scala 3.0.0 compiler, and it works as shown.

WARNING: However, notice that this particular example is flawed, because a string like "one" will not convert to an integer — it will throw an exception. If you want to use this technique, make sure you account for exceptions. In this case you might use a try/catch block during the conversion attempt.

Also note that this example is shown here in the Scala Book.

The earlier Scala 2 implicit conversion function syntax

This was an earlier Scala 2 (and/or Dotty) approach that has been changed for Scala 3:

import MyImplicitMethods.personToEmployee
import scala.language.implicitConversions

object ImplicitsTest1 extends App {

    val p = Person("Barb Saxet")
    val e: Employee = p

    println(s"Employee: $e")

}

object MyImplicitMethods {
    implicit def personToEmployee(p: Person): Employee = Employee(p.name)
}

case class Employee (name: String)
case class Person (name: String)

As shown, I import scala.language.implicitConversions to make this work. That’s needed if you want to get rid of this Scala/Dotty compiler warning message:

[info] Compiling 1 Scala source to /Users/al/ImplicitConversions/target/scala-0.11/classes ...
[warn] -- Feature Warning: /Users/al/Projects/ImplicitConversions/Test1.scala:9:22 
[warn] 9 |    val e: Employee = p
[warn]   |                      ^
[warn]   |Use of implicit conversion method personToEmployee in object MyImplicitMethods should be enabled
[warn]   |by making the implicit value scala.language.implicitConversions visible.
[warn]   |This can be achieved by adding the import clause 'import scala.language.implicitConversions'
[warn]   |or by setting the compiler option -language:implicitConversions.
[warn]   |See the Scala docs for value scala.language.implicitConversions for a discussion
[warn]   |why the feature should be explicitly enabled.
[warn] -- Feature Warning: /Users/al/Projects/ImplicitConversions/Test1.scala:16:17 
[warn] 16 |    implicit def personToEmployee(p: Person): Employee = Employee(p.name)
[warn]    |                 ^
[warn]    |Definition of implicit conversion method personToEmployee should be enabled
[warn]    |by making the implicit value scala.language.implicitConversions visible.
[warn]    |This can be achieved by adding the import clause 'import scala.language.implicitConversions'
[warn]    |or by setting the compiler option -language:implicitConversions.
[warn]    |See the Scala docs for value scala.language.implicitConversions for a discussion
[warn]    |why the feature should be explicitly enabled.
[warn] two warnings found

Note that you get that warning because, as the implicit conversions page on the Scala Tour states, “implicit conversions can have pitfalls if used indiscriminately.”

This post is sponsored by my new book,
Learn Functional Programming Without Fear.