Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since somebody has to say something, I followed the instructions at <code>Importers</code> to try to compile the tree before returning it.</p> <p>If you give the compiler plenty of stack, it will correctly report the error.</p> <p>(It didn't seem to know what to do with the switch annotation, left as a future exercise.)</p> <pre><code>apm@mara:~/tmp/bigmethod$ skalac bigmethod.scala ; skalac -J-Xss2m biguser.scala ; skala bigmethod.Test Error is java.lang.RuntimeException: Method code too large! Error is java.lang.RuntimeException: Method code too large! biguser.scala:5: error: You ask too much of me. Console println s"5 =&gt; ${BigMethod.lookup(5)}" ^ one error found </code></pre> <p>as opposed to</p> <pre><code>apm@mara:~/tmp/bigmethod$ skalac -J-Xss1m biguser.scala Error is java.lang.StackOverflowError Error is java.lang.StackOverflowError biguser.scala:5: error: You ask too much of me. Console println s"5 =&gt; ${BigMethod.lookup(5)}" ^ </code></pre> <p>where the client code is just that:</p> <pre><code>package bigmethod object Test extends App { Console println s"5 =&gt; ${BigMethod.lookup(5)}" } </code></pre> <p>My first time using this API, but not my last. Thanks for getting me kickstarted.</p> <pre><code>package bigmethod import scala.language.experimental.macros import scala.reflect.macros.Context object BigMethod { // For this simplified example we'll just make some data up. //final val size = 700 final val size = 7000 val mapping = List.tabulate(size)(i =&gt; (i, i + 1)) def lookup(i: Int): Int = macro lookup_impl def lookup_impl(c: Context)(i: c.Expr[Int]): c.Expr[Int] = { def compilable[T](x: c.Expr[T]): Boolean = { import scala.reflect.runtime.{ universe =&gt; ru } import scala.tools.reflect._ //val mirror = ru.runtimeMirror(c.libraryClassLoader) val mirror = ru.runtimeMirror(getClass.getClassLoader) val toolbox = mirror.mkToolBox() val importer0 = ru.mkImporter(c.universe) type ruImporter = ru.Importer { val from: c.universe.type } val importer = importer0.asInstanceOf[ruImporter] val imported = importer.importTree(x.tree) val tree = toolbox.resetAllAttrs(imported.duplicate) try { toolbox.compile(tree) true } catch { case t: Throwable =&gt; Console println s"Error is $t" false } } import c.universe._ val switch = reify(new scala.annotation.switch).tree val cases = mapping map { case (k, v) =&gt; CaseDef(c.literal(k).tree, EmptyTree, c.literal(v).tree) } //val res = c.Expr(Match(Annotated(switch, i.tree), cases)) val res = c.Expr(Match(i.tree, cases)) // before returning a potentially huge tree, try compiling it //import scala.tools.reflect._ //val x = c.Expr[Int](c.resetAllAttrs(res.tree.duplicate)) //val y = c.eval(x) if (!compilable(res)) c.abort(c.enclosingPosition, "You ask too much of me.") res } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload