The elidable annotation takes an Int parameter which specifies the priority of the method. The lower the integer the more likely the method would be removed during compilation. The elidable object defines several values that are used in our example.
When compiling with the -Xelide-below
To try the following example copy the example into a scala file (elidable.scala for example) and compile as indicated below:
- package example
- import scala.annotation.elidable
- import scala.annotation.elidable._
- object ElidableExamples {
- @elidable(ALL) def all = println("all")
- @elidable(ASSERTION) def assertion = println("assertion")
- @elidable(CONFIG) def config = println("config")
- @elidable(FINE) def fine = println("fine")
- @elidable(FINER) def finer = println("finer")
- @elidable(FINEST) def finest = println("finest")
- @elidable(INFO) def info = println("info")
- @elidable(OFF) def off = println("off")
- @elidable(SEVERE) def severe = println("severe")
- @elidable(WARNING) def warning = println("warning")
- }
- object Main extends Application {
- println("starting")
- import ElidableExamples._
- all
- assertion
- config
- fine
- finer
- finest
- info
- off
- severe
- warning
- println("ending")
- assert(false, "boom!")
- }
Output from scalac elidable.scala && scala example.Main
starting
assertion
off
ending
java.lang.AssertionError: assertion failed: boom!
at scala.Predef$.assert(Predef.scala:93)
at example.Main$.(elidable.scala:34)
at example.Main$.(elidable.scala)
at example.Main.main(elidable.scala)
Output from scalac -Xelide-below 0 elidable.scala && scala example.Main
starting
assertion
config
fine
finer
finest
info
off
severe
warning
ending
java.lang.AssertionError: assertion failed: boom!
at scala.Predef$.assert(Predef.scala:93)
at example.Main$.(elidable.scala:34)
at example.Main$.(elidable.scala)
at example.Main.main(elidable.scala)
Output from scalac -Xelide-below 1000 elidable.scala && scala example.Main
starting
assertion
off
severe
ending
java.lang.AssertionError: assertion failed: boom!
at scala.Predef$.assert(Predef.scala:93)
at example.Main$.(elidable.scala:34)
at example.Main$.(elidable.scala)
at example.Main.main(elidable.scala)
Output from scalac -Xelide-below 3000 elidable.scala && scala example.Main
starting
off
ending
This sounds great, hadn't heard of it, but what does it do to line numbers when attaching to those classes with elided blocks at runtime?
ReplyDeletePatrick
A good question :)
ReplyDeleteI have not checked but I would think that it would be a bug if the remaining line numbers did not correctly line up. I think the Scala compiler guys are pretty used to line number tracking
There was a discussion about using class hierarchy to select what should be elided. It's an interesting idea.
ReplyDeleteAt least in Scala 2.8.1, require (in contrast to assert and assume) is not annotated in Predef with @elidable(ASSERTION).
ReplyDelete