Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala case class too many fields

Tags:

java

scala

I am trying to define one case class with 1000 fields in scala REPL 2.11.8. The case class definition is like:

case class Step2_Class(
   `Response` : String,
   `D1` : String,
   `D2` : String,
   `D3` : String,
   `D4` : String,
    //......,
   `D999` : String,
   `D1000` : String)

And the REPL is waiting for response. After about 1 hour, following stack overflow exception is thrown out.

java.lang.StackOverflowError
    at scala.reflect.internal.Trees$class.traverseComponents$1(Trees.scala:1294)
    at scala.reflect.internal.Trees$class.itraverse(Trees.scala:1330)
    at scala.reflect.internal.SymbolTable.itraverse(SymbolTable.scala:16)
    at scala.reflect.internal.SymbolTable.itraverse(SymbolTable.scala:16)
    at scala.reflect.api.Trees$Traverser.traverse(Trees.scala:2475)
    at scala.reflect.internal.Positions$DefaultPosAssigner.traverse(Positions.scala:288)
    at scala.reflect.internal.Positions$DefaultPosAssigner.traverse(Positions.scala:282)
    at scala.reflect.internal.Trees$class.traverseComponents$1(Trees.scala:1283)
    at scala.reflect.internal.Trees$class.itraverse(Trees.scala:1330)

Do you have any ideas? Does scala not support such case? Are there any work-arounds?

like image 858
Guifang Zhou Avatar asked Dec 12 '16 08:12

Guifang Zhou


2 Answers

It seems that the error is not related to the number of fields.

You said that it breaks after some time. Are you sure that you haven't a recursive function without a return condition? The StackOverFlow infact is a runtime error.

Thrown when a stack overflow occurs because an application recurses too deeply.

If the problem is related to the number of fields it appears at compile time.

While the problem is not related to the number of fields it is a very bad idea to create a code with 1000 parameters, or a class with 1000 fields. As you can imagine there is no real difference between D1 and D15 and D1000, so why don't use an array called D?

like image 152
Davide Lorenzo MARINO Avatar answered Sep 30 '22 14:09

Davide Lorenzo MARINO


Until recently there was a limit of 22 fields in a case class. So it isn't surprising if the compiler (or REPL itself) fails to gracefully handle 1000 fields. You can try to give it more memory (and stack memory in particular) by setting JAVA_OPTS (-Xss for stack overflow in particular), e.g.

JAVA_OPTS="$JAVA_OPTS -Xss8M" scala

but you'd have to guess how much, it wouldn't help much with speed (unless it's mostly stuck garbage collecting, which is quite possible), and it could fail with more fields.

like image 36
Alexey Romanov Avatar answered Sep 30 '22 15:09

Alexey Romanov