title: How to dynamically bind variable when using scala.tools.nsc.interpreter.IMain
body: <p>I'm trying to implement a class evaluating the result of given scala script and input value and print the result of evaluation with scala.tools.nsc.interpreter.IMain. Here's my test code.</p>
  
  <pre><code>package some.test.package
  
  import javax.script.{Compilable, CompiledScript}
  
  import scala.tools.nsc.Settings
  import scala.tools.nsc.interpreter.{IMain, JPrintWriter}
  
  class Evaluator {
  
    // Create IMain instance when created
    val settings = new Settings()
    settings.usejavacp.value = true
    val writer = new JPrintWriter(Console.out, true)
    val engine = new IMain(settings, writer)
    val compiler = engine.asInstanceOf[Compilable]
  
    var inputVal : Int = _
    var compiledObj : CompiledScript = _
  
    // compile given script
    def compileScript(givenCode : String) {
      compiledObj = compiler.compile(givenCode)
    }
  
    // evaluate
    def evalCompiled(): Unit = {
      compiledObj.eval()
    }
  
    // set input value
    def setInput(givenInput:Int) {
      inputVal = givenInput
    }
  
    // bind input variable
    def bindInput() {
      engine.bind("inputVal", "Int", inputVal)
    }
  
  }
  
  object IMainTest {
  
    def main(args:Array[String]): Unit = {
      // create an instance
      val evaluator = new Evaluator()
  
      // first set input value to 3 and do evaluation
      evaluator.setInput(3)
      evaluator.bindInput()
      evaluator.compileScript("def f(x:Int):Int=x+1; println(f(inputVal))")
      evaluator.evalCompiled()
  
      // let's change input value and re-evaluate
      evaluator.setInput(5)
      evaluator.evalCompiled()
  
    }
  }
  </code></pre>
  
  <p>My expectation was the program prints out '4' and '6' but the result was '4' and '4' printed out.</p>
  
  <p>So, my question is...</p>
  
  <ol>
  <li>Does IMain.bind just copies value not reference to some place that the engine can refer to?</li>
  <li>Should I compile every time when I want to re-assign value of variable?</li>
  <li>If the answer for 1 and 2 is 'YES', then is there any other way that I can achieve my purpose without re-bind and re-compile? I just expected re-assigning input value does not need additional bind operation or compile operation.</li>
  </ol>
  
  <p>I think it's really hard to find IMain related examples or documents so any reference links might be also very helpful for me. Thank you.</p>