2016-07-15 31 views
1

我想嘗試OCaml的FFI並結合一些C的功能類似於 ncurses的教程,但我選擇了ecore_evas(EFL)代替:OCaml的建立自己的錯誤:int類型,而不是類型單位

這裏的函數我想用:

void ecore_main_loop_quit(void) 
void ecore_main_loop_begin(void) 
int ecore_evas_init(void) 
int ecore_evas_shutdown(void) 
Ecore_Evas * ecore_evas_new(const char *, int, int, int, int, const char *) 
ecore_evas_title_set(const Ecore_Evas *, const char *) 
ecore_evas_show(ee) 
ecore_evas_free(ee) 

所以我創建文件ecore_evas.ml本:

open Ctypes 
open Foreign 
let ecore_main_loop_begin = foreign "ecore_main_loop_begin" (void @-> 
returning void) 
let ecore_main_loop_quit = foreign "ecore_main_loop_quit" (void @-> 
returning void) 
let ecore_evas_init = foreign "ecore_evas_init" (void @-> returning int) 
let ecore_evas_shutdown = foreign "ecore_evas_shutdown" (void @-> 
returning int) 
type ecore_evas = unit ptr 
let ecore_evas : ecore_evas typ = ptr void 
let ecore_evas_new = foreign "ecore_evas_new" (string @-> int @-> int 
@-> int @-> int @-> string @-> returning ecore_evas) 
let ecore_evas_title_set = foreign "ecore_evas_title_set" (ecore_evas 
@-> string @-> returning void) 
let ecore_evas_show = foreign "ecore_evas_show" (ecore_evas @-> 
returning void) 
let ecore_evas_free = foreign "ecore_evas_free" (ecore_evas @-> 
returning void) 

然後我創建了這些命令的MLI文件:

corebuild -pkg ctypes.foreign ecore_evas.inferred.mli 
cp _build/ecore_evas.inferred.mli ./ 

的ecore_evas.inferred.mli

val ecore_main_loop_begin : unit -> unit 
val ecore_main_loop_quit : unit -> unit 
val ecore_evas_init : unit -> int 
val ecore_evas_shutdown : unit -> int 
type ecore_evas = unit Ctypes.ptr 
val ecore_evas : ecore_evas Ctypes_static.typ 
val ecore_evas_new : bytes -> int -> int -> int -> int -> bytes -> 
ecore_evas 
val ecore_evas_title_set : ecore_evas -> bytes -> unit 
val ecore_evas_show : ecore_evas -> unit 
val ecore_evas_free : ecore_evas -> unit 

我開了一ecore_evas_window.ml有:

open Ecore_evas 
let() = 
ecore_main_loop_begin(); 
ecore_evas_init(); 
let ee = ecore_evas_new "" 50 50 300 300 "" in 
ecore_evas_title_set ee "This is a test"; 
ecore_evas_show ee 

然後我試着用

corebuild -pkg ctypes.foreign -lflags -cclib,-lecore_evas 
ecore_evas_window.native 

但是編譯我有這個錯誤信息:

+ ocamlfind ocamlc -c -w A-4-33-40-41-42-43-34-44 -strict-sequence -g 
-bin-annot -short-paths -thread -package ctypes.foreign -package core 
-ppx 'ppx-jane -as-ppx' -o ecore_evas_window.cmo ecore_evas_window.ml 
File "ecore_evas_window.ml", line 4, characters 2-21: 
Error: This expression has type int but an expression was expected of type 
unit 
Command exited with code 2. 
Hint: Recursive traversal of subdirectories was not enabled for this build, 
as the working directory does not look like an ocamlbuild project (no 
'_tags' or 'myocamlbuild.ml' file). If you have modules in 
subdirectories, 
you should add the option "-r" or create an empty '_tags' file. 

To enable recursive traversal for some subdirectories only, you can 
use the 
following '_tags' file: 

true: -traverse 
<dir1> or <dir2>: traverse 

Compilation unsuccessful after building 4 targets (3 cached) in 00:00:00. 

此消息清楚地表明,這個問題是關係到線 ecore_evas_init();,但我真的不明白爲什麼會產生這個錯誤。

編輯 由於@hcarty原始問題已經解決,但完整的代碼無法正常工作。如果需要,可以找到工作版本here

回答

2

ecore_evas_init返回int。該行上的;隱含地表示「左邊的表達返回unit」。由於ecore_evas_init()返回一個整數,這些類型不匹配。你必須提供一些簡單的選擇:

  1. 與替換現有行:

    let _ = ecore_evas_init() in 
    

    將忽略返回值。

  2. 執行對返回值的一些檢查:

    assert (ecore_evas_init() = 0); (* Or whatever the "success" status is *) 
    
  3. 明確忽略結果:

    ignore(ecore_evas_init()); 
    

    這大致相當於let _ = ...

在一個完整的應用程序你可能會想要更復雜的錯誤但是這應該足以繼續這個示例應用程序。

+0

我已經使用了'ignore'指令。你可以添加你的答案。對於像我這樣的初學者來說,這會很有幫助。 – cedlemo

+0

好的建議 - 補充 – hcarty

相關問題