ocaml不在脚本模式下加载.ocamlinit [英] ocaml doesn't load .ocamlinit in script mode

查看:90
本文介绍了ocaml不在脚本模式下加载.ocamlinit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,当我在终端中以交互模式运行ocaml时,它将加载.ocamlinit.但是,当我在脚本模式下运行ocaml test.ml时,它不会加载.ocamlinit文件.这实际上给我造成了一些麻烦,因为我在.ocamlinit中设置了以下设置:

I find that when I run ocaml in terminal, ie, in interactive mode, it will load .ocamlinit. However, when I run ocaml test.ml, ie, in script mode, it doesn't load .ocamlinit file. This actually causes some trouble for me, since I have the following setup in my .ocamlinit:

let () =
  try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
  with Not_found -> ()
;;

#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;

因此,当我以交互方式运行时,将执行#use "topfind",并且可以在我的代码中打开其他库.但是由于.ocamlinit不在脚本模式下加载,因此当我在终端中运行ocaml test.ml并使用test.ml时,如下所示:

So when I run in interactive mode, #use "topfind" will be executed and I can open other libraries in my code. But as .ocamlinit is not loaded in script mode, when I run ocaml test.ml in terminal, with test.ml like this:

打开Core.Std ;;

open Core.Std;;

let()= print_endline你好,世界"

let () = print_endline "hello world"

它将失败,并显示错误:"Error: Unbound module Core".

It will fail with error: "Error: Unbound module Core".

我的问题是:

  1. 如何解决此问题,以便在脚本模式下运行ocaml时,也会加载.ocamlinit文件?
  2. 为什么ocaml顶级系统无法以脚本模式加载.ocamlinit文件?
  1. How to fix this so that when running ocaml in script mode, the .ocamlinit file will also be loaded?
  2. Why the ocaml toplevel system doesn't load .ocamlinit file in script mode?

ps. OCaml 版本是4.01.0

推荐答案

我认为这是顶层的错误"(至少是意外行为).这可能会在OCaml的未来版本中修复.除了ocaml -noprompt < test.ml(这不是等效的,因为您将从顶级获得一些嘈杂的输出,而不仅仅是执行文件)之外,我没有一个令人满意的解决方案,或者当然编译程序并运行它(可能就像ocamlbuild test.byte && ./test.byte一样简单.

I think that's a "bug" (or at least unexpected behaviour) in the toplevel. This could be fixed in future versions of OCaml. I don't have a satisfying workaround to propose, besides ocaml -noprompt < test.ml (which is not equivalent as you will get some noisy output from the toplevel instead of just your file executed), or of course compiling the program and running it (which may be as simple as ocamlbuild test.byte && ./test.byte).

这篇关于ocaml不在脚本模式下加载.ocamlinit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆