1

I am new to Scala and I have a case class like this:

case class Student(
 name: String,
 age: Option[Int] = None,
 id: String
)

and another case class like this:

case class Member(
 id: String,
 `type`: String,
 name: String
)

I want to create a helper implicit in such a way that:

case class Member(
 id: String,
 `type`: String,
 name: String
)

object Member {
  implicit def toStudent(member:Member):Student = Student(member.name,None,member.id)
}

and then call the method like this:

val newMember = Member("abc","Student","John")
val student = newMember.toStudent

or something fancy like that. Would this be possible in Scala as I see a lot of fancy syntax similar to this at a lot of places?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Shivam Sahil
  • 4,055
  • 3
  • 31
  • 62

1 Answers1

3

In Scala 3 you can use extensions methods:

// ...

extension (member: Member)
  def toStudent: Student = Student(member.name, None, member.id)

val newMember = Member("abc", "Student", "John")
val student = newMember.toStudent

For Scala 2 you can define explicit conversion:

import scala.language.implicitConversions
object Member {
  implicit def toStudent(member: Member): Student =
    Student(member.name, None, member.id)
}

val newMember = Member("abc", "Student", "John")
val student = newMember: Student

Or using implicit class decorator:

implicit class MemberDecorator(val member: Member) extends AnyVal {
  def toStudent(): Student = Student(member.name, None, member.id)
}

val newMember = Member("abc", "Student", "John")
val student = newMember.toStudent()
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • I see but currently my whole production code is on Scala 2.13 – Shivam Sahil Nov 09 '22 at 11:05
  • Hmm I tried what you shared but its not working, the implicit that you have declared is it supposed to be placed in helper object or where? If I put it in helper object it fails and says cannot find the method I think you can create those implicits only within a class – Shivam Sahil Nov 09 '22 at 11:41
  • 1
    @ShivamSahil GuruStron's code compiles https://scastie.scala-lang.org/DmytroMitin/VD5LfcQUTo6wb06oErvukQ/1 https://scastie.scala-lang.org/DmytroMitin/VD5LfcQUTo6wb06oErvukQ/2 – Dmytro Mitin Nov 09 '22 at 11:53
  • 1
    @ShivamSahil [Where does Scala look for implicits?](https://stackoverflow.com/questions/5598085/where-does-scala-look-for-implicits) – Dmytro Mitin Nov 09 '22 at 11:54
  • @DmytroMitin, I see thank you very much for the entire code in playground. I see you do the conversion as: `val student = newMember: Student` however I was trying out: `newMember.toStudent` But thanks anyways, this is what I needed. – Shivam Sahil Nov 09 '22 at 12:00
  • 2
    @ShivamSahil Can't you see `newMember.toStudent` at the 2nd link https://scastie.scala-lang.org/DmytroMitin/VD5LfcQUTo6wb06oErvukQ/2 ? – Dmytro Mitin Nov 09 '22 at 12:24
  • @DmytroMitin Oh sorry I missed it but now I see... However the implicit declaration is supposed to be in some object right? I had a standalone scala file where I had these case classes and I was trying to put `implicit class` there and that's what was throwing me an error but thank you very much again! Now I understand it completelt – Shivam Sahil Nov 09 '22 at 12:54
  • 2
    @ShivamSahil Preferably not some object but companion object https://scastie.scala-lang.org/DmytroMitin/VD5LfcQUTo6wb06oErvukQ/3 Please read "Where does Scala look for implicits?" – Dmytro Mitin Nov 09 '22 at 13:06