This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Interpreter Problem

2 replies
Gabriel Cardoso
Joined: 2011-07-06,
User offline. Last seen 42 years 45 weeks ago.
Bonjour,
I am playing with the scala interpreter and here is the problem I encountered :
The code I would like to compile : 
scala> import scala.tools.nsc.interpreter.IMainimport scala.tools.nsc.interpreter.IMain
scala> val i = new IMaini: scala.tools.nsc.interpreter.IMain = scala.tools.nsc.interpreter.IMain@fbe496b
scala> val s = "val f = (x : Int) => x*2\nval g = (x : Int) => x*3"  s: java.lang.String = val f = (x : Int) => x*2 val g = (x : Int) => x*3
scala> i.interpret (s)f: (Int) => Int = <function1> g: (Int) => Int = <function1>res15: scala.tools.nsc.interpreter.Results.Result = Success
scala> val f = i.valueOfTerm ("f").get.asInstanceOf [Int => Int] f: (Int) => Int = <function1>
scala> val g = i.valueOfTerm ("g").get.asInstanceOf [Int => Int]g: (Int) => Int = <function1>
scala> f(2) //should be 4 right??res16: Int = 6
scala> g(2)res17: Int = 6
Looks like a bug... Am I doing something wrong?
Thanks in advance,
Cheers
Gabriel


Gabriel Cardoso
Joined: 2011-07-06,
User offline. Last seen 42 years 45 weeks ago.
Re: Interpreter Problem
Problem solved by interpreting values separately, as follow : 
scala> i.interpret ("val f = (x : Int) => x*2") f: (Int) => Int = <function1>res20: scala.tools.nsc.interpreter.Results.Result = Success
scala> i.interpret ("val g = (x : Int) => x*3") g: (Int) => Int = <function1>res21: scala.tools.nsc.interpreter.Results.Result = Success
scala> val f = i.valueOfTerm ("f").get.asInstanceOf [Int => Int] f: (Int) => Int = <function1>
scala> val g = i.valueOfTerm ("g").get.asInstanceOf [Int => Int]g: (Int) => Int = <function1>
scala> f (2)res22: Int = 4
scala> g (2)res23: Int = 6

2011/7/20 Gabriel Cardoso <gcardoso [dot] w [at] gmail [dot] com>
Bonjour,
I am playing with the scala interpreter and here is the problem I encountered :
The code I would like to compile : 
scala> import scala.tools.nsc.interpreter.IMainimport scala.tools.nsc.interpreter.IMain
scala> val i = new IMaini: scala.tools.nsc.interpreter.IMain = scala.tools.nsc.interpreter.IMain@fbe496b
scala> val s = "val f = (x : Int) => x*2\nval g = (x : Int) => x*3"  s: java.lang.String = val f = (x : Int) => x*2 val g = (x : Int) => x*3
scala> i.interpret (s)f: (Int) => Int = <function1> g: (Int) => Int = <function1>res15: scala.tools.nsc.interpreter.Results.Result = Success
scala> val f = i.valueOfTerm ("f").get.asInstanceOf [Int => Int] f: (Int) => Int = <function1>
scala> val g = i.valueOfTerm ("g").get.asInstanceOf [Int => Int]g: (Int) => Int = <function1>
scala> f(2) //should be 4 right??res16: Int = 6
scala> g(2)res17: Int = 6
Looks like a bug... Am I doing something wrong?
Thanks in advance,
Cheers
Gabriel



Philippe Lhoste
Joined: 2010-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Interpreter Problem

On 20/07/2011 12:15, Gabriel Cardoso wrote:
> Problem solved by interpreting values separately, as follow :

See my questions in my answer: http://permalink.gmane.org/gmane.comp.lang.scala.user/42658

Looks like valueOfTerm doesn't like to have multiple values to be defined in the same
interpreter run.
Bug or "undocumented feature"? I don't know...

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland