首页 文章

OCaml类型gtk类型错误视图

提问于
浏览
1

在错误中你看到了类型

?start:GText.iter - >?stop:GText.iter - >?slice:bool - >?visible:bool - > unit - > string

是不是与字符串相同的类型?因为该函数返回一个字符串,并且它传递给的函数需要一个字符串 . 我认为最终的最后一个类型 - >是返回值的类型 . 我错了吗?

ocamlfind ocamlc -g -package lablgtk2 -linkpkg calculator.ml -o calculator
File "calculator.ml", line 10, characters 2-44:
Warning 10: this expression should have type unit.
File "calculator.ml", line 20, characters 2-54:
Warning 10: this expression should have type unit.
File "calculator.ml", line 29, characters 61-86:
Error: This expression has type
         ?start:GText.iter ->
         ?stop:GText.iter -> ?slice:bool -> ?visible:bool -> unit -> string
       but an expression was expected of type string

Compilation exited abnormally with code 2 at Sun Aug  2 15:36:27

试着打电话之后

(* Button *)
  let button = GButton.button ~label:"Add"
                              ~packing:vbox#add () in
  button#connect#clicked ~callback: (fun () -> prerr_endline textinput#buffer#get_text ());

它说它是字符串 - >单位,并暗示我错过了一个;

这些编译器错误有点令人困惑 .

编辑:显然称它为按钮#connect #clicked~callback :( fun() - > prerr_endline(textinput#buffer#get_text()));是正确的,我需要在函数周围放置(...)以使prerr知道textinput ...()是一个带()的函数作为arg而不是2个参数传递给prerr .

这很有趣 . 谢谢您的帮助 .

2 回答

  • 0

    这个类型:

    ?start:GText.iter - >?stop:GText.iter - >?slice:bool - >?visible:bool - > unit - > string

    是一个返回字符串的函数 . 它与字符串不同 .

    如类型所示,如果您将 () 作为参数传递,则可以获取字符串 . 还有4个可选参数 .

  • 1

    当函数包含可选参数时,它必须至少包含一个非可选参数 . 否则,不可能区分部分应用和实际应用,即功能调用 . 如果所有参数都是可选的,那么通过约定,将 unit 类型的必需参数添加到结尾 . 所以,你的功能,实际上是"waits",直到你提供 () 说:"all is done, use default for everything else" .

    TL; DR;将 () 添加到函数调用的末尾,显示错误 .

    ?start:GText.iter -> ?stop:GText.iter -> ?slice:bool -> ?visible:bool -> unit -> string
                                                                              ^^^^
                                                                        required argument
                                                                           of type unit
    

    更新

    新问题是,你忘了用括号分隔它:

    prerr_endline (textinput#buffer#get_text ())
    

    否则,编译器会查看它,因为您为 prerr_endline 提供了3个参数 .

相关问题