0

I have some case classes:

case class Address(street : String, state : String, zip : Option[Int])

case class Person(name : String, address : Option[Address], children : Seq[Person])

and a function that returns one of these classes:

def getPerson() : Person

I want to make a version of this function that can be called from Java:

def getPersonJava() : JavaPerson

where JavaPerson would have the same fields as the case class but the Option would be java 8 Optional and the Seq would be java.util.List or some more suitable java collection type.

And when the user calls getPersonJava().address they would get an instance of a class where the fields had also been transformed.

Is there a way to transform nested case classes like this using macros or something so that I don't have to manually write the conversions? I cannot change the case classes or the getPerson function.

mushroom
  • 6,201
  • 5
  • 36
  • 63
  • 1
    How about using Java instead and patching some things on top of it with implicits? – Reactormonk Oct 10 '15 at 20:57
  • You mean use Java classes instead of the case classes? I should have been clearer in the question, but I can't actually change the case classes or `getPerson`. I am just trying to make a function that wraps `getPerson` and makes it more convenient to use from Java. – mushroom Oct 10 '15 at 21:02
  • I think you can do that with macros. Another idea (that is not typesafe however) is to use sbt sourceGenerators and create these Java style case classes that based on the Scala case classes (turn Seq to List etc). The generated case classes will be created with an apply method that take the original case class and turns it to the Java style case class. – Vered Rosen Oct 11 '15 at 20:06

0 Answers0