summaryrefslogtreecommitdiff
path: root/test/pending/run/reify_closure7.scala
blob: 8933df23fadf375fd60ef665d6038679e9959a4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import scala.tools.nsc.reporters._
import scala.tools.nsc.Settings
import reflect.runtime.Mirror.ToolBox

object Test extends App {
  var q = 0
  var clo: Int => Int = null
  def foo[T](ys: List[T]): Int => Int = {
    val z = 1
    var y = 0
    val fun: reflect.Code[Int => Int] = x => {
      y += 1
      q += 1
      println("q = " + q)
      println("y = " + y)
      x + ys.length * z + q + y
    }

    if (clo == null) {
      val reporter = new ConsoleReporter(new Settings)
      val toolbox = new ToolBox(reporter)
      val ttree = toolbox.typeCheck(fun.tree)
      val dyn = toolbox.runExpr(ttree)
      clo = dyn.asInstanceOf[Int => Int]
    }

    clo
  }

  println("first invocation = " + foo(List(1, 2, 3))(10))
  println("second invocation = " + foo(List(1, 2, 3, 4))(10))
}