In the code below there are 2 case classes Person and Employee. There is a function called
emp which takes in an Employee and returns a string. Instead I pass a person while defining an implicit def for converting a Person to Employee.
case class Person(fName : String, lName : String, dob : Date, gender : String) case class Employee(fName : String, lName : String, dob : Date, gender : String, department : String) implicit def toEmployee(person : Person) : Employee = Employee(fName = person.fName, lName = person.lName, dob = person.dob, gender = person.gender, department = "Fire Squad") val aPerson = Person(fName = "John", lName = "Doe", gender = "Male", dob = new Date()) def emp(employee: Employee) : String = ??? emp(aPerson)
But was this the actual purpose for the introduction of implicits? Or was it to support type classes? If it was to support type classes should the above code be changed to introduce an adh-hoc method called
toEmployee for the class
Person. And then
emp can be called using
Which the correct way to use implicits ?