An implicit conversion function example in Scala/Dotty

I’m short on time today, so I’ll just share this little example of how to write an implicit conversion function in Scala/Dotty:

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