How to create a scala class based on user input?

579 views Asked by At

I have a use case where I need to create a class based on user input.

For example, the user input could be : "(Int,fieldname1) : (String,fieldname2) : .. etc" Then a class has to be created as follows at runtime

Class Some
{
   Int fieldname1
   String fieldname2
   ..so..on..
}

Is this something that Scala supports? Any help is really appreciated.

3

There are 3 answers

3
lutzh On

No, not really.

The idea of a class is to define a type that can be checked at compile time. You see, creating it at runtime would somewhat contradict that.

You might want to store the user input in a different way, e.g. a map.

What are you trying to achieve by creating a class at runtime?

1
dhg On

Your scenario doesn't seem to make sense. It's not so much an issue of runtime instantiation (the JVM can certainly do this with reflection). Really, what you're asking is to dynamically generate a class, which is only useful if your code makes use of it later on. But how can your code make use of it later on if you don't know what it looks like? For example, how would your later code know which fields it could reference?

2
Julian Peeters On

I think this makes sense, as long as you are using your "data model" in a generic manner.

Will this approach work here? Depends.

If your data coming from a file that is read at runtime but available at compile time, then you're in luck and type-safety will be maintained. In fact, you will have two options.

  1. Split your project into two:

    • In the first run, read the file and write the new source programmatically (as Strings, or better, with Treehugger).

    • In the second run, compile your generated class with the rest of your project and use it normally.

  2. If #1 is too "manual", then use Macro Annotations. The idea here is that the main sub-project's compile time follows the macro sub-project's runtime. Therefore, if we provide the main sub-project with an "empty" class, members can be added to it dynamically at compile time using data that the macro sees at runtime. - To get started, Modify the macro to read from a file in this example

Else, if you're data are truly only knowable at runtime, then @Rob Starling's suggestion may work for you as it did me. I'll share my attempt if you want to be a guinea pig. For debugging, I've got an App.scala in there that shows how to pass strings to a runtime class generator and access it at runtime with Java reflection, even define a Scala type alias with it. So the question is, will your new dynamic class serve as a type-parameter in Slick, or fail to, as it sometimes does with other libraries?