Apart from serious performance problems, Scala is a very powerful language. Therefore I am now using it frequently for scripted tasks inside Bash. Is there a way to just execute a *.scala file exactly the way I can do with Python files? As far as I know, Python uses bytecode to execute programs, exactly like the JVM does. However, there is not anything called pythonc (like scalac or javac) I need to call in order to accomplish this. Hence I expect Scala to be able to act in a similar manner.
-
1Possible duplicate of http://stackoverflow.com/questions/7620144 ? – Richard Sitze Jul 19 '13 at 23:57
-
2I'm not sure what quantitative meaning to attach to "serious," but Scala approximates the performance of other JVM languages when used without particular attention to performance and is very competitive with Java when you apply appropriate optimizations to empirically determined hot-spots. Of course, if you include JVM start-up time, it does not compare favorably with native code or even conventional interpreted languages such as Python or Perl. But when you compile the code every time you run a "script," it would try anyone's patience—Don't do that. – Randall Schulz Jul 20 '13 at 18:25
2 Answers
The scala man page provides some examples on how to run Scala code fragments as if they were a script, for both Windows and non-Windows platforms (below examples copied from the man page):
Unix
#!/bin/sh
exec scala "$0" "$@"
!#
Console.println("Hello, world!")
argv.toList foreach Console.println
Windows
::#!
@echo off
call scala %0 %*
goto :eof
::!#
Console.println("Hello, world!")
argv.toList foreach Console.println
To speed up subsequent runs you can cache the compiled fragment with the -savecompiled
option:
#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#
Console.println("Hello, world!")
argv.toList foreach Console.println
Update: as of Scala 2.11 (as noted in this similar answer), you can now just do this on Unix:
#!/usr/bin/env scala
println("Hello, world!")
println(args.mkString(" "))
I don't use python, but in Scala, the most scripty thing I can do is this:
thinkpux:~/proj/mini/forum > echo 'println(" 3 + 4 = " + (3 + 4))' | scala
Welcome to Scala version 2.10.2 (Java HotSpot(TM) Server VM, Java 1.7.0_09).
Type in expressions to have them evaluated.
Type :help for more information.
scala> println(" 3 + 4 = " + (3 + 4))
3 + 4 = 7
scala> thinkpux:~/proj/mini/forum >
However, afterwards, I don't have visual feedback in the bash, so I have to call 'clear'.
But there is no problem in writing a script and executing that:
thinkpux:~/proj/mini/forum > echo 'println(" 3 + 4 = " + (3 + 4))' > print7.scala
thinkpux:~/proj/mini/forum > scala print7.scala
3 + 4 = 7
Then, there aren't issues with the shell.
With an enclosing class, the code wouldn't be executed:
thinkpux:~/proj/mini/forum > echo -e 'class Foo {\nprintln(" 3 + 4 = " + (3 + 4))\n}\n'
class Foo {
println(" 3 + 4 = " + (3 + 4))
}
thinkpux:~/proj/mini/forum > scala Foo.scala
thinkpux:~/proj/mini/forum > cat Foo.scala
class Foo {
println(" 3 + 4 = " + (3 + 4))
}
But with instatiating a class, you can execute code in it, without using the wellknown (hope so) 'main' way:
thinkpux:~/proj/mini/forum > echo -e 'class Foo {\nprintln(" 3 + 4 = " + (3 + 4))\n}\nval foo = new Foo()' > Foo.scala
thinkpux:~/proj/mini/forum > cat Foo.scala
class Foo {
println(" 3 + 4 = " + (3 + 4))
}
val foo = new Foo()
thinkpux:~/proj/mini/forum > scala Foo.scala
3 + 4 = 7

- 35,537
- 11
- 75
- 121