I have mentioned this in an earlier discussion, but since we are now allowing side-effecting top-level val
s, it only makes sense to allow side-effecting top-level statements
package p
println("Hello World!")
This does several things:
-
Cleans up the syntax for something that is already allowed via
val meaninglessName = println("Hello World")
, just without the meaningless names. This is a big wart with the meaningless_ <- foo
syntax in for-comprehensions, for example. -
Converges
*.scala
file syntax closer towards Ammonite/Script/Worksheet/Mill*.sc
syntax or SBTâs*.sbt
syntax -
Converges the REPL and
*.scala
syntax -
Makes Scala easier to learn for novices: you can just write code and put it into a file, and run it. Just like Python, which is in many cases the gold-standard for ease-of-getting-started for programming beginners
Note that ânovicesâ doesnât just mean students: there is a large class of data scientists, analysts, system admins, devops, mathematicians, mechanical engineers, and others who would fall under the category of âprofessional novicesâ. These are people who use programming to supplement their main job but have neither the need nor desire to become experts in it. These are the sorts of people who would benefit greatly from being able to âjust write code and run itâ
Even within the existing Scala community, there is tons of evidence for existing demand for such a syntax: SBT, Mill, IntelliJ Worksheets, Scala-Fiddle, Scastie, Jupyter-Scala, and others have each re-invented their own flavor of it. Clearly, this is something which a lot of people want.
We have already had discussions about how to simplify the main method and make Scala less boilerplatey and easier to pick up for absolute beginners, who may be confused by def main(args: Array[String]): Unit = {...}
. Regardless of what elegant syntax people can come up with, no-syntax-at-all beats everything in terms of boilerplate (zero) and things to learn (zero), not to mention similarity with other getting-started languages (Python, Ruby, Javascript, âŚ). And we get this essentially for free, by removing an arbitrary restriction that allows val
s but not free-floating expressions/statements at the top of a file.
If we hope for Scala to be as approachable as the other languages that currently dominate the ease-of-learning, getting-started, and professional-novice scene, we should do what they do where-ever it makes sense, and this is one of them. In this case, the fact that it simplifies Scala itself in multiple different ways, narrowing the arbitrary differences in Scala syntax seen in different contexts, is just a (huge) bonus.