2

As per the Scala tutorials, we need to compile the Scala code using scalac filename.scala before executing it using scala filename. But when I tried like scala filename.scala, it runs and got the output.

Why is it so? So compiling using scalac is not required to run the code? Could someone please explain this.

Thanks.


scala -help was really helpful. It says

    A file argument will be run as a scala script unless it contains only
    self-contained compilation units (classes and objects) and exactly one
    runnable main method.  In that case the file will be compiled and the
    main method invoked.  This provides a bridge between scripts and standard
    scala source.

Thanks Dennis for the pointer.

3
  • Scala can be used as a sort of scripting language and from a newer version on (can't remember which) , this is possible , running the code without even defining a class or an object Commented Dec 6, 2014 at 15:48
  • @DoomProg Interesting. Are you referring to Scala Worksheet when you said "running the code without even defining a class or an object"? Commented Dec 6, 2014 at 17:20
  • 1
    @crackjack not only. Try making a file with just println("hello") and run it with scala command. If you have scala 2.10 and up I think you should be able to get the message. Commented Dec 6, 2014 at 19:36

2 Answers 2

5

running the scala command actually compiles it first behind the scenes, then runs the resulting program.

Sign up to request clarification or add additional context in comments.

4 Comments

So, as you say, there should be class files generated. These files are used to run. But I couldn't find any class files, generated on current directory. So where should be this class files residing?
@Maximin in main memory
@sschaef Thanks. It would be great if I get some links or pointer to docs explaining this. It's interesting, almost all the tutorials I covered missed to quote this.
@Maximin I don't think it's on memory. The ScriptRunner class calls compiler with the temp directory path which is deleted up on JVM exits. The temp directory path varies for OS. Also you can save jar file with -save option.
2

This behavior is explained in the help for the scala command.

You can get the help by executing the scala -help command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.