When to use semicolons in SML?

冷暖自知 提交于 2019-12-03 23:36:56

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!