2015-12-28 32 views
0

我可能會考慮頂級錯誤的方式。作爲項目的一部分,維護一系列頂級指令的首選方法是什麼?在ml文件中嵌入頂級指令

是否有辦法在OCaml源文件中包含像#install_printer這樣的頂級指令,以便在程序編譯時被忽略,但在運行時由頂層執行?當編譯程序時,即使指令本身被忽略,我也希望類型檢查指令的理想狀態,例如,

type 'a with_infinity = Finite of 'a | Infinite 

let print_int_with_infinity pp item = 
(match item with 
    | Infinite -> Format.pp_print_string pp "Infinite" 
    | Finite i -> Format.pp_print_int pp i) 

(* install printer cannot occur in this context *) 
#install_printer print_int_with_infinity 

回答

2

沒有這樣做的預定義方式,但是您可以讓預處理器在編譯時刪除指令。

對於類型檢查的圖元,你能做的最好的就是它預處理爲類似

#install_printer some_function

let _ = (some_function:Format.formatter -> 'a -> unit)

2

一個好方法與頂層是工作在項目的根目錄中有一個.ocamlinit文件。從同一目錄啓動utopocaml時會加載此文件。

它通常是這樣的:

 
#use "topfind";; 
#require "this";; 
#require "that";; 

let _printer = ...;; 
#install_printer _printer;; 
... 

與此相關的,如果環境變量OCAMLPATH設置爲/path/to/my/project:...,並有一個適當的META文件/path/to/my/project/foo,則有可能加載項目 - 本地foo庫及其依賴關係使用#require "foo"

+0

你的建議是 - 像往常一樣:) - 非常有價值的馬丁,這應該是被接受的答案! –