println("Hi Mum") in a
.scala file at top-level is a horror show waiting to happen. Now, you raise the issue of the semantics of the scripty scala form. There seem to be two issues here that I think are orthogonal:
- default environment
- evaluation semantics
The default environment issue I think can be addressed by either having specific extensions (e.g.
.am) or bundling them up into a standardised environment import e.g.
import ammonite.environment, much like the language feature flags are. The mechanic through which this works is plumbing, and largely booring (e.g. it could be importing a
scala.language.ScriptEnvironment instance with a well-named macro that injects the environment).
As for the evaluation semantics, there appear to be two of them. The first one runs the statements just as you would in
main. The other collects a dictionary (or bag) of values associated with names, and then makes this available to some down-stream process. In the case of interactive worksheets, this dictionary is used to decorate the IDE with the evaluated values. In ammonite or the scala repl, it gives you interactive values to play with as you continue to type. In SBT, this dictionary becomes the parameterised build commands/environment. But fundamentally it’s the same deal - you evaluate each statement, and record a memoisation of the result against any identifier, minting a new one if the statement is anonymous. The
main semantics then reduces to the special case where you decline to do anything with that dictionary, and run it purely for the side-effects.