You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, the compiler (scalac) strangely does not accept shebangs.
$ scalac Hello.scala
Hello.scala:1: error: expected class or object definition
#!/usr/bin/env sh
^
Hello.scala:2: error: expected class or object definition
exec scala "$0" "$@"
^
Hello.scala:3: error: expected class or object definition
!#
^
three errors found
This creates an artificial distinction between Scala scripts and Scala programs, forcing programmers to choose one or the other. And when a programmer uses someone else's code, he'd rather not have to manually add or remove shebang lines to make scala and scalac happy.
Fortunately, the solution is simple: Have scalac consider shebang lines as comments. Anything between #! and !# is just a comment.
The text was updated successfully, but these errors were encountered:
@retronym said:
A change to the Scala parser (even just for a new comment syntax) would need to be first discussed on the scala-language mailing list. There are many tools upstream (IDEs, editors) that are affected by such a change, so it isn't taken lightly.
Closing this as "out of scope" pending such discussion.
The interpreter (scala) allows shebangs for convenience. For example:
Hello.scala:
#!/usr/bin/env sh
exec scala "$0" "$@"
!#
object Hello {
def main(args: Array[String]) {
println("Hello World!")
}
}
$ chmod a+x Hello.scala
$ ./Hello.scala
Hello World!
However, the compiler (scalac) strangely does not accept shebangs.
$ scalac Hello.scala
Hello.scala:1: error: expected class or object definition
#!/usr/bin/env sh
^
Hello.scala:2: error: expected class or object definition
exec scala "$0" "$@"
^
Hello.scala:3: error: expected class or object definition
!#
^
three errors found
This creates an artificial distinction between Scala scripts and Scala programs, forcing programmers to choose one or the other. And when a programmer uses someone else's code, he'd rather not have to manually add or remove shebang lines to make scala and scalac happy.
Fortunately, the solution is simple: Have scalac consider shebang lines as comments. Anything between #! and !# is just a comment.
The text was updated successfully, but these errors were encountered: