Cory Klein
Cory Klein

Reputation: 55660

Executing a Scala code file with the intepreter

Here is my sample source code:

// Broken.scala
def fibFrom(a: Int, b: Int): Stream[Int] = a #:: fibFrom(b, a + b)
println(fibFrom(0,1).take(10).toList)

When I run these statements individually in the interpreter, everything works fine.

scala> def fibFrom(a: Int, b: Int): Stream[Int] = a #:: fibFrom(b, a + b)
fibFrom: (a: Int, b: Int)Stream[Int]

scala> println(fibFrom(0,1).take(10).toList)
List(0, 1, 1, 2, 3, 5, 8, 13, 21, 34)

However, when I pass the file itself to the interpreter, it hangs.

$ scala Broken.scala
^C
$

The scala -help documentation indicates that Scala source is a valid input to scala, so what am I doing wrong?

Upvotes: 0

Views: 46

Answers (1)

Randall Schulz
Randall Schulz

Reputation: 26486

It works fine for me. I think you may be encountering a problem with the scala command trying to start the background compiler, which is how Scala scripts are handled.

This is a problem that happens on certain OSes or with certain security software / settings. From [1]:

There are two things you may want to look at. First, hostname must resolve to your host's IP address -- even if it's 127.0.0.1. Second, there may be an fsc daemon running causing the trouble. Try running fsc -shutdown.

[1] http://grokbase.com/t/gg/scala-user/12bkn165h9/cannot-execute-hello-world-script

Upvotes: 2

Related Questions