19

When I run something like the following from the command line, what really happens?

> scala hello.scala

Is there a hello.class generated, executed, and then discarded? Or does Scala behave somehow like an interpreter in this case? I am just thinking that, of course, I cannot do the same for Java:

> java hello.java
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adrian
  • 3,762
  • 2
  • 31
  • 40

1 Answers1

24

Yes, there is a hello.class generated. The compiler will wrap your code inside a Main object, compile it then execute Main.main, given hello.scala of

println(args.mkString)
println(argv.mkString)

If you run with the -Xprint:parser option: scala -Xprint:parser hello.scala foo bar you'll see how the code gets rewritten:

package <empty> {
  object Main extends scala.ScalaObject {
    def <init>() = {
      super.<init>();
      ()
    };
    def main(argv: Array[String]): scala.Unit = {
      val args = argv;
      {
        final class $anon extends scala.AnyRef {
          def <init>() = {
            super.<init>();
            ()
          };
          println(args.mkString);
          println(argv.mkString)
        };
        new $anon()
      }
    }
  }
}

This code is then compiled (I believe to a memory filesystem - but I'm not sure) and executed. Looking at ScriptRunner, I see that a temporary directory is created under the default temp folder. For instance looking at my system, I see a bunch of %TEMP%/scalascript* folders.

Note that even in the interpreter, the code is not interpreted. See Scala: Is there a default class if no class is defined? for more info (it's really being rewritten, compiled and evaluated).

Community
  • 1
  • 1
huynhjl
  • 41,520
  • 14
  • 105
  • 158
  • How does the compiler make `val args = argv`? What if one types `println(arguments.mkString)`? Will there be a `val arguments = argv`? – Peter Schmitz Oct 05 '11 at 11:20
  • @PeterSchmitz, `args` and `argv` are hard-coded, see https://github.com/scala/scala/blob/master/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala#L358 and around line 388. Those names are simply conventions from C (`argv`) and Java programming (`args`). If you type `println(arguments.mkString)` you'll get a compiler error because `arguments` is not defined. – huynhjl Oct 05 '11 at 14:10
  • Thanks, didn´t know that. Though I´m a bit surprised! As far as I learned "hard-coded" isn´t good in general... – Peter Schmitz Oct 05 '11 at 16:28
  • @Peter it's just an identifier, like the entry method is called `main`, and the method for printing something is called `println`. I don't see the problem or why you would want it any other way... after all it's got to be called something – Luigi Plinge Oct 05 '11 at 18:55
  • @LuigiPlinge Acknowledged, `println` and especially `main` are also "hard-coded", but I was confused that a method argument name like `args` was "hard-coded" then (but I´m fine with that, there has to be a name for `args`, because not explicitly statable), which I can choose when having the main method in class filled by myself instead of been constructed by the REPL(or perhaps extending `App`). Everything´s fine :D – Peter Schmitz Oct 05 '11 at 20:48
  • How can there be a hello.class with a class called Main? – nafg Apr 16 '13 at 04:44
  • @nafg, yeah technically the class is not called `hello.class`. As you see from `-Xprint:parser`, the class may be called `Main$`. I took the question to be, is there a class that is compiled, run and discarded? Not specifically is it called `hello.class`... – huynhjl Apr 16 '13 at 05:58
  • So if it really creates those files won't two scala scripts overwrite each other? – nafg Apr 17 '13 at 20:40
  • @nafg, the class files are created into a temporary directory under `System.getProperty("java.io.tmpdir")`. See https://github.com/scala/scala/blob/2.10.1/src/compiler/scala/tools/nsc/ScriptRunner.scala#L105. The directory is cleaned up on the script exit. – huynhjl Apr 18 '13 at 04:26
  • @huynhjl So you can't run two scripts at the same time? – nafg Apr 19 '13 at 05:24
  • I think you can. It will just create the class files into different directories (the directories are suffixed with some random/unique digits specific to that run)... – huynhjl Apr 19 '13 at 05:33