Maybe we need a "-c" option for scala command?

Just like perl or ruby has, to check if there is any syntax problem in the script.

$ ruby -c ruby-hash.rb 
Syntax OK

Thank you.

Interpreted (or loosely) code needs to differentiate compilation and execution as by default it is always execution.
in case of compiled language, compilation(where we can find whether syntax is ok or not) and execution are always different steps and no-way, one can combine them(ok, sbt/gradle combines them but that is only for experimentation…)

Scala actually also supports scripting, the scala command can be passed a scala script file to run, which has slightly different rules (e.g. most things are allowed at the top level). See e.g. this tutorial.

I think, the usual way for this is to use an editor with Scala support, aside from IDEs there are several that can use the Metals language server, see their homepage for setup for various editors.

You can also use the scala command with the -Ystop-after option to abort after a specific compiler phase. The last one relevant to syntax checking is probably refchecks:

scala -Ystop-after:refchecks script.scala

This should show all compile errors, but not run the script if there aren’t any.


Perl really needs that option because it is a dynamically typed scripting language. Scala is a statically typed compiled language. It always goes through a compile phase before the code is run, so that option isn’t nearly as needed. If there is a syntax error, it will be shown and the code won’t run. Even in the scripting environment that @crater2150 mentions, the code is compiled before it is run.

I will note that the nature of scripting in Scala is being changed in Scala 3. Now script files need a function that is annotated with @main. While I worry a bit about the overhead of this, it has a very nice advantage that arguments to the main function are parsed from the command line automatically.

1 Like

I think scala -c would make a nice alias for scalac, or vice-versa, where -c is the short option for --compile-only.

I thought about recommending scalac, but it always produces a class file. The only way I found to disable that is to use -Ystop-after, which also works for checking only with scala. Is there some option I’ve overlooked? Or is that what you’re suggesting?

Yes, I tire of scalac -d /tmp, because I wind up with undesirable collisions when scala -cp /tmp. The problems are “unsupported tasty format” when switching compiler versions and files are read too eagerly.

Scala 2 and 3 both can stop after typer,refchecks,erasure but even more convenient would be -Ystop-before:codegen.

I was thinking of scala -c just to mean compile but don’t run, which is also what scalac is for. Class files can go wherever they usually go (where I can’t see them).

Actually I just got an exception with -Ystop-after:erasure

âžś  scala -Ystop-after:erasure hello.scala
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "" because "p" is null
        at scala.reflect.internal.SymbolTable.phase_$eq(SymbolTable.scala:243)
        at scala.reflect.internal.SymbolTable.pushPhase(SymbolTable.scala:247)

I think there are a few generally useful checks that come late, such as for mixins.