打开 Module_name 给出编译器错误
我无法编译一个极其简单的 ocaml 程序 test2.ml,
open Test1
print_string " Hello "
其中 test1.ml 仅包含 1 行
type program = string
并且 test1.ml 已编译:
bash-3.2$ ocamlc test1.ml
bash-3.2$ ls test1.*
test1.cmi test1.cmo test1.ml
有人知道为什么 test1.ml 不编译吗?谢谢。
更多信息。这很奇怪,因为如果我注释掉 test2.ml 的第一行“open ...”或 如果我注释掉它的第三行“print_string...”,但它们不能共存!
I cannot compile an extremely simple ocaml program test2.ml
open Test1
print_string " Hello "
with test1.ml containing only 1 line
type program = string
And test1.ml is compiled:
bash-3.2$ ocamlc test1.ml
bash-3.2$ ls test1.*
test1.cmi test1.cmo test1.ml
Anyone know why test1.ml does not compile?? Thank you.
More info. It's quite strange because, test2.ml compiles if I comment out its first line "open ..." OR
if I comment out its 3rd line "print_string..." but they cannot coexist!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
打印您收到的错误会很有帮助。仅供参考:
原因有点复杂。文件的正常语法是一系列顶级语句,例如类型定义、let(不带
in
)、模块定义/打开/包含等在。像
print_string "Hello"
这样的表达式永远不会被视为顶级语句,除非含义完全明确,这 99% 的情况下需要使用将它们与前面和后面的语句分开; ;
因此,您可以编写以下内容:
它会起作用。不过,大多数时候,最好通过将表达式转换为顶级
let
来保持文件干净:这还有一个好处是确保函数返回
unit< /code>,拥有它总是很高兴。
Printing the error you received would have been helpful. For the reference, it's:
The reason for this is a bit complex. The normal syntax is for a file to be a sequence of top-level statements, such as type definitions,
let
(withoutin
), module definition/opening/including and so on.Expressions such as
print_string "Hello"
are never treated as top-level statements unless the meaning is completely unambiguous, which 99% of the time involves separating them from the previous and following statement with a;;
So, you could write the following:
And it would work. Most of the time, though, it is preferable to keep the file clean by turning the expression into a top-level
let
:This also has the benefit of making sure that the function returns
unit
, which is always nice to have.