为什么在顶层之外的某些语句中需要双分号 [英] Why do I need a double semicolon in some statements outside the toplevel
问题描述
我有以下两行代码:
let a = [1;2;3;4;5;6;7;8;9;0]
print_string (String.concat " " (List.map string_of_int a))
它给了我这个错误:
文件test.ml",第 65 行,字符 0-0:错误:语法错误
File "test.ml", line 65, characters 0-0: Error: Syntax error
但是,如果我以双分号结束该行:
However, if I end the line with a double semicolon:
let a = [1;2;3;4;5;6;7;8;9;0];;
print_string (String.concat " " (List.map string_of_int a))
效果很好.
我读过,双分号只能用在顶层,所以我为什么要在这里使用它.如果我不这样做,我应该写什么?
I've read, double semicolon should only be used in the top level, so why do I need it here. If I don't, what should I have written instead?
推荐答案
可以使用双分号来引入顶级表达式:
Double semicolons can be used to introduce a toplevel expression:
let x = 0
let do_something () = Printf.printf "x=%d" x
;; do_something ()
;; do_something ()
这里没有分隔 ;;
第一个 do_something()
仍然是函数`do_something 定义的一部分.
Here without the separating ;;
the first do_something ()
would still be part of the definition of the function `do_something.
通常认为使用单元顶级绑定更惯用:
It is generally considered more idiomatic to use an unit toplevel binding:
let x = 0
let do_something () = Printf.printf "x=%d" x
let () =
do_something (); do_something ()
这篇关于为什么在顶层之外的某些语句中需要双分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!