I know that semicolons are used as terminators in REPL. But I'm confused about when to use them in a source file.
For example it is not necessary after val x = 1
. But if I omit it after use "foo.sml"
, the compiler will complain about it.
Then, what are the rules on using semicolons?
Semicolons are used for a number of syntactic entities in SML. They are normally used to create sequences of, e.g., expressions or declarations. Here's a link to the SML grammar:
http://www.mpi-sws.org/~rossberg/sml.html
In your case, you are interested in the semicolon for declarations (the dec class). Note that the semicolon that creates a sequence of decs is optional. You never actually need it when writing SML modules, and it is rare to see them. For example
not
In a source file, the only place you normally use a semicolon is separating expressions that have side-effects. For example,
but this usage is rare.
The smlnj repl only evaluates declarations when it sees a semicolon though, so you should use a semicolon whenever you want to see or play with the value. I think the
use "foo.sml";
case is confusing because it's not a declaration; it's an expression. I imagine that the repl converts expressions likeuse "foo.sml"
intoval _ = use "foo.sml"
. Thus it needs the semicolon to tell the repl to really run it, as above. As a side note, there is nothing special aboutuse
. It is simply a function of typestring -> unit
.