How to use 'scala script'?

Perhaps I misunderstand the 'scala script' option (it doesn't seem to be documented in the product help), but I tried creating a new file with just a function definition like:

def foo(msg: String) {}


or even just a single line file like:

println("Hello world")


...but when trying to compile I get an compilation error:

scala: expected class or object definition
def hello(msg: String) {
^



I'd assumed that a 'scala <scriptfile>' would let me create scripts that could be run from the command line, or via 'scala' (as opposed to the 'salac' compiler). Such command line scripts do not require me to define any classes or objects and execute fine with the scala command line.

Is there documentation or can someone guide me on what a 'scala script' actually is and how to use that feature of the scala plugin?

By default, the 'Scala Script' run configuration includes a 'make' step.. perhaps I just need to remove that step or perhaps there's a make setting that understands 'script' mode where I shouldn't have to create objects or classes?
2 comments

hmm, can't seem to edit posts?

Anyway, I'm running IntelliJ 12.0.2 and scala 2.10.0

0

You have few possible workarounds for this problem:
1. Use Scala Worksheet. It have .sc extention, so it won't be compiled.
2. Move your script outside of source root. Then compiler won't compile it, but probably you will get problems with dependencies.
3. Don't use external compiler (not external compiler is deprecated and probably will be removed, but before this you still can you it)

In external compilation there is no way to analyze file and to understand that it's script file. Additionally it's not really simple to compile such project with script files using any other build system, because it doesn't contain scripting feature.

Best regards,
Alexander Podkhalyuzin.

0

Please sign in to leave a comment.