问题描述
可以在 Scala 中编写脚本.所以你可以把它放到 Hello.scala
One can write scripts in Scala. So you can put this into Hello.scala
#!/bin/sh
exec scala $0 $@
!#
println("You supplied " + args.length + " arguments!")
并使其在 Unix 中可执行
and make it executable in Unix by
chmod u+x Hello.scala
然后你可以简单地运行脚本
Then you can run the script simply by
./Hello.scala
如果没有语法错误,这将编译脚本并运行它.但是,这并没有考虑到我只想在不执行脚本的情况下进行语法检查的情况.我不想修改脚本(即通过删除 #! 指令)并且我不想生成任何 *.class 文件.
This compiles the script and runs it if there are no syntax errors. However, this does not account for situation when I only want to syntax check without executing the script. I do not want to modify the script (i.e. by removing the #! directive) and I do not want any *.class files to be generated.
如何检查 Scala 脚本的语法?
How can I syntax check a Scala script?
推荐答案
我希望您实际上想要的不仅仅是检查正确的语法……大概您想知道的是,如果您确实编译了它.这涉及类型检查和语法检查.
I expect that you actually want a little more than just checking for correct syntax ... presumably what you want to know is that your file would compile correctly if you did actually compile it. That involves type checking as well as syntax checking.
对于 Scala 源文件(即不是脚本),您可以指定 -Ystop:refchecks 命令行参数来使编译器在开始代码生成之前停止(如果您真的只对语法正确性感兴趣,则可以指定 -Ystop:解析器).如果有错误,它们会以与完全编译源代码完全相同的方式显示在控制台上.
For Scala source files (ie. not scripts) you can specify the -Ystop:refchecks command line argument to cause the compiler to stop before it starts code generation (if you really are only interested in syntactic correctness you could specify -Ystop:parser). If there are errors they will be shown on the console in exactly the same way as if you fully compiled the sources.
对于 Scala 脚本,您还可以指定 -Ystop:refchecks 参数.如果您这样做,那么您将在控制台上看到报告的编译错误,或者,如果脚本中没有错误,您将看到以下内容,
For Scala scripts you can also specify the -Ystop:refchecks argument. If you do this, then you will either see compile errors reported on the console or, if there are no errors in the script, you will see the following,
$ scala -Ystop:refchecks Hello.scala
java.lang.ClassNotFoundException: Main
ClassNotFoundException 表明没有生成类文件并且您的脚本没有被执行.
The ClassNotFoundException indicating that no classfiles have been generated and that your script has not been executed.
这篇关于如何在不执行脚本和生成任何类文件的情况下检查 Scala 脚本的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!