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
structure S = struct
val x = 5
fun f x = x
val z = x + x
end
not
structure S = struct
val x = 5;
fun f x = x;
val z = x + x
end
In a source file, the only place you normally use a semicolon is separating expressions that have side-effects. For example,
val x = ref 5
val _ = (x := !x + 1; x := !x+ 2)
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 like use "foo.sml"
into val _ = 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 about use
. It is simply a function of type string -> unit
.
来源:https://stackoverflow.com/questions/19237803/when-to-use-semicolons-in-sml