Conversion
case class
case class Person(name: String)
def greet(): String = s"Hello, $name"
given fromStringToPerson: Conversion[String, Person] = Person(_)
@main def run =
println("John".greet())
Scope
The conversion
rules are the same as the given
rules
imported conversion
current scope
companion object
Exercises
Exercise 1
case class Person(name: String)
def greet(): String = s"Hello, $name"
- Write a conversion function from Person to Int that calculate the length of the name
- Put it in a separate
object
- Use it with an
import
Exercise 2
case class User(name: String)
def login(): String = s"Logged in: $name"
- Write a conversion from Person to User
- Write a conversion from User to Person
- Test both