Take the 2-minute tour ×

Please excuse the funny title, I am using it in analogy with "zip bomb". Is it possible to create a scala source file, that will, when compiled, produce a large number of class files (or a very large single class file)? Is there any way the size of the class files could grow faster than linearly with the size of the source file?

share|improve this question
What in hell could you possibly need that for? –  Nikita Volkov Dec 18 '11 at 11:48
@NikitaVolkov It sounds like an interesting mental exercise, but I have a problem trying to find a practical application. –  Vatine Dec 18 '11 at 11:55
Ok, for those people who need pratical applications: Trying to protect a service that compiles Scala code from DOS attacks. –  Kim Stebel Dec 18 '11 at 12:00
What a fun question! –  Synesso Dec 18 '11 at 12:11
@Kim, this does't really answer the stated question, but your best bet to protect your service is to have some kind of watchdog that just kills the compiler after x seconds or y MB. –  josh3736 Dec 18 '11 at 17:59
show 2 more comments

1 Answer

up vote 29 down vote accepted

Specialization is inherently exponential in the number of type parameters specialized.

class Huge[@specialized A, @specialized B, @specialized C](
  val a: A, val b: B, val c: C
) {} // 730 files, 2.9 MB

class Gigantic[@specialized A, @specialized B, @specialized C, @specialized D](
  val a: A, val b: B, val c: C, val d: D
) {} // 6562 files, 26 MB

Pattern matching can also involve a lot of code duplication for complex cases (though I find it difficult to predict exactly when this will occur).

share|improve this answer
Ah, I've never used specialization, great answer! –  Kim Stebel Dec 18 '11 at 13:09
Rex, for info, there is a bug raised for the pattern matching issues.scala-lang.org/browse/SI-1133, it fails because too much code is generated. This should be fixed with the new virtual pattern matcher in 2.10 though. –  Matthew Farwell Dec 18 '11 at 13:27
add comment

Your Answer


