2

I use Scala 2.9.1. I have a simple scala "interpreter":

import scala.tools.nsc.interpreter.IMain
import scala.tools.nsc.interpreter.Results.Result
import scala.tools.nsc.interpreter.Results.Success

object App {

  def main(args: Array[String]) {
    val interpreter = new IMain
    val result:Result = interpreter.interpret(args(0))
    result.toString() match {
      case "Success" =>
        {
          var success = result.asInstanceOf[Success]
          println(success.productElement(0))
        };
      case _ => println("very bad result");
    }
  }

}

When i try to compile it (maven) i get:

[ERROR] /home/koziolek/workspace/dsi/src/main/scala/pl/koziolekweb/scala/dsi/App.scala:15: error: not found: type Success
[INFO]           var success = result.asInstanceOf[Success]

As you can see, the compiler said that there is no type Success, although I imported it.

0__
  • 66,707
  • 21
  • 171
  • 266
Koziołek
  • 2,791
  • 1
  • 28
  • 48

1 Answers1

7

Success is an object, not a class, you would need to cast it to its singleton type result.asInstanceOf[Success.type]. Obviously you are trying to work around not knowing how to do the pattern match. That would allow you to get the right result without casting:

import tools.nsc.interpreter.Results._

result match {
   case Success    => "yes!"
   case Error      => "no..."
   case Incomplete => "you missed something"
}

If you want to get the resulting value of the interpreted expression in the case of success, see my reply in this post for more details.

Community
  • 1
  • 1
0__
  • 66,707
  • 21
  • 171
  • 266