-2

i want to create case class dynamically and for that i wrote program but not able to define case class as program return String type instead of defining class.

eg. case class + " " + $class_name " (" + $col_list + ")"

expected: defined class abc

  • 1
    Dynamic code generation is not very common in Scala and especially in a distributed environment like spark might quite hard to get correct. What is the problem you try to solve originally? – Harald Gliebe Jul 01 '19 at 13:18
  • I wonder if one can create an instance of the repl and can pass strings into it though. – Alexander Oh Jul 01 '19 at 14:09
  • possible dupulicate: https://stackoverflow.com/questions/39142979/dynamic-code-evaluation-in-scala#39143272 it looks like there is a IMain interpreter. – Alexander Oh Jul 02 '19 at 08:54

1 Answers1

0

Warning: you need to be 100% sure that the code you are generating compiles.

You can do this:

val yourPath = ...\\somePath.scala 
new PrintWriter(yourPath) {

  write(s"case class $class_name($col_list)")
  close()
}

This way you create a scala class parsing Strings. If the class generates wrong it wont compile, so you need to be sure what you are generating.

Pedro Correia Luís
  • 1,085
  • 6
  • 16