Types OCaml型gtk+;类型错误视图

Types OCaml型gtk+;类型错误视图,types,gtk,ocaml,Types,Gtk,Ocaml,在下面的错误处,您可以看到类型 ?开始:GText.iter-> ?停止:GText.iter->?切片:bool->?可见:bool->unit->string 这不是和string相同的类型吗?因为函数返回一个字符串,而传递给它的函数需要一个字符串。我认为final->之后的最后一个类型是返回值的类型。我弄错了吗 ocamlfind ocamlc -g -package lablgtk2 -linkpkg calculator.ml -o calculator File "calculato

在下面的错误处,您可以看到类型

?开始:GText.iter-> ?停止:GText.iter->?切片:bool->?可见:bool->unit->string

这不是和string相同的类型吗?因为函数返回一个字符串,而传递给它的函数需要一个字符串。我认为final->之后的最后一个类型是返回值的类型。我弄错了吗

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 ());
它说它是string->unit类型,并表示我缺少一个

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

编辑:显然像按钮一样调用它#连接#点击~callback:(fun()->prerr#u endline(textinput#buffer#get#text());是正确的,我需要在函数周围放置(…),以使prerr知道textinput…()是一个以()作为参数的函数,而不是将2个参数传递给prerr

这很有趣。感谢您的帮助。

此类型:

?开始:GText.iter->?停止:GText.iter->?切片:bool->?可见:bool->单元->字符串

是一个返回字符串的函数。它和字符串不一样


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

当函数包含可选参数时,它必须至少包含一个非可选参数。否则,就无法区分部分应用程序和实际应用程序,即函数调用。如果所有参数都是可选的,则按照惯例,在末尾添加类型为
unit
的必需参数。因此,您的函数实际上是“等待”,直到您提供了
()
语句:“所有操作都完成了,其他所有操作都使用默认值”

TL;博士将显示错误的
()
添加到函数调用的末尾

 ?start:GText.iter -> ?stop:GText.iter -> ?slice:bool -> ?visible:bool -> unit -> string
                                                                          ^^^^
                                                                    required argument
                                                                       of type unit
更新 新的问题是,您忘记了用括号分隔它:

prerr_endline (textinput#buffer#get_text ())

否则,当您向
prerr\u endline
提供3个参数时,编译器会查看它

prerr_endline textinput#buffer#get_text())是我使用它的方式。这不应该打印传递到prerr_endline的函数返回的字符串吗?好的,这开始有意义了。我想在尝试做类似的事情之前,我可能需要阅读更多关于函数的内容。