2

I'm newly at Scala.

I want to run function which is wrote at my file while.scala:

def whileLoop {
    var i = 1
    while(i <= 3) {
        println(i)
        i += 1
    }
}
whileLoop

here is how it looks at REPL:

scala> scala /home/nazar/Downloads/while.scala
<console>:1: error: illegal start of simple expression

If I understood right from book. I need to specify .scala file location and run with:

scala /path/to/file.scala

Why it fails?

Here is snippet from book:

You can run the code like this:
batate$ scala code/scala/while.scala
1
2
3

UPDATE:

I tried options :load it works, but when I want to run another file for_loop.scala:

def forLoop {
    println( "for loop using Java-style iteration" )
    for(i <- 0 until args.length) {
        println(args(i))
    }
}    
forLoop

it fails:

scala> :load /home/nazar/Downloads/for_loop.scala
Loading /home/nazar/Downloads/for_loop.scala...
<console>:9: error: not found: value args
           for(i <- 0 until args.length) {
                            ^
<console>:8: error: not found: value forLoop
              forLoop
              ^

scala> :load /home/nazar/Downloads/for_loop.scala hello scala
That file does not exist

How to solve this trouble?

catch23
  • 17,519
  • 42
  • 144
  • 217
  • What version of Scala are you using? That works fine for me in Scala 2.10.4. – Randall Schulz Apr 11 '14 at 14:38
  • @RandallSchulz `Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0)` – catch23 Apr 11 '14 at 14:40
  • 1
    Re: UPDATE: That's because there's no `args` in the REPL. You only get that for free-standing scripts, either with explicit use of the `scala` command or as shown in the 2nd answer to http://stackoverflow.com/questions/17756775/scripting-with-scala-how-to-launch-an-uncompiled-script, as I mentioned below. – Randall Schulz Apr 11 '14 at 17:48

2 Answers2

5

You do it like this, from the shell / command line, not from within the REPL (% is nominal shell prompt):

% scala MyScalaScriptName.scala

You did this:

% scala
scala> scala while.scala
<console>:1: error: illegal start of simple expression

The only thing known within the Scala REPL is Scala code itself and a few built-in special commands. However, one of them will do what you want:

% cd
% scala
scala> :load Downloads/while.scala
Loading Downloads/while.scala
1
2
3
Randall Schulz
  • 26,420
  • 4
  • 61
  • 81
  • Can you explain more? what is wrong here, and how to solve it? coz I couldn't figure out what is wrong here? – catch23 Apr 11 '14 at 14:46
  • with load it works. Which ways can I use here to run scala code? – catch23 Apr 11 '14 at 15:02
  • In the REPL `:load` is your only option. From the command line, you can do as shown above or put a bit of boilerplate into a file header to make a file act as if it's executable (as with other interpreted scripts in the Unix world). The 2nd answer at http://stackoverflow.com/questions/17756775/scripting-with-scala-how-to-launch-an-uncompiled-script shows how to do this. – Randall Schulz Apr 11 '14 at 15:37
  • i updated questions. Can you explain why doesn't work here? It looks the same - Only that I need to specify arguments. But it prints that could't find file.... – catch23 Apr 11 '14 at 17:38
2

In order to use command line arguments (via args) it is needed to extend App or more commonly to define a principal or main method in an object, namely a def main(args: Array[String]) method, which defines an entry point to the program. Consider for instance

object AnyName extends App {
  def forLoop {
    println( "for loop using Java-style iteration" )
    for(i <- 0 until args.length) {
        println(args(i))
    }
  }
  forLoop
}

AnyName.main(Array("first","second"))

Try to load it from REPL. The other approach is thus,

object AnyName {
  def main(args: Array[String]) {
    println( "for loop using Java-style iteration" )
    for(i <- 0 until args.length) {
        println(args(i))
    }
  }
}

AnyName.main(Array("first","second"))

In this latter example, note the scope of args, bound to the parameter in main.

elm
  • 20,117
  • 14
  • 67
  • 113