[英]OCaml type gtk+ type error view
在错误的下方,您会看到类型
?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 ());
它说它是string类型-> unit并提示我缺少一个;
这些编译器错误令人困惑。
编辑:显然调用它像button#connect#clicked〜callback:(fun()-> prerr_endline(textinput#buffer#get_text())); 是正确的,我需要在函数周围加上(...),以使prerr知道textinput ...()是一个以()作为arg的函数,而不是将2个参数传递给prerr。
这很有趣。 谢谢您的帮助。
这种类型:
?start:GText.iter->?stop:GText.iter->?slice:bool->?visible:bool-> unit-> string
是一个返回字符串的函数。 它与字符串不同。
如类型所示,如果传递()
作为参数,则可以获得字符串。 还有4个可选参数。
当一个函数包含可选参数时,它必须至少包含一个非可选参数。 否则,不可能区分部分应用程序和实际应用程序,即函数调用。 如果所有参数都是可选的,则按照约定,将类型为unit
的必需参数添加到末尾。 因此,您的函数实际上“等待”,直到您提供()
为止:“所有操作都完成了,对其他所有操作都使用默认值”。
TL; DR; 将add ()
添加到函数调用的末尾,显示错误。
?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个参数时,编译器会查看它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.