简体   繁体   中英

OCaml type gtk+ type error view

Down there at the Error you see the type

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

isn't that the same type as string? because the function returns a string and the function it is being passed to requires a string. I thought that the last type after the final -> was the type of the return value. Am I mistaken?

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

After trying to call

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

it says it is type string -> unit and suggests i am missing a ;

these compiler errors are kind of confusing.

Edit: apparently calling it like button#connect#clicked ~callback: (fun () -> prerr_endline (textinput#buffer#get_text ())); was correct and I needed to put (...) around the function to make prerr know that textinput...() was a function with () as the arg and not 2 arguments being passed to prerr.

This is pretty fun. Thanks for the help.

This type:

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

Is a function that returns a string. It's not the same as string.

As the type shows, you can get a string if you pass () as an argument. There are also 4 optional arguments.

When a function contains optional arguments it must contain at least one non-optional argument. Otherwise it is impossible to distinguish between partial application and actual application, ie, function invocation. If all arguments are optional, then by a convention an required argument of type unit is added to the end. So, your function, actually "waits" until you provide the () saying: "all is done, use default for everything else".

TL;DR; add () to the end of your function call, that shows the error.

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

Update

The new problem, is that you've forgotten to delimit it with parenthesis:

prerr_endline (textinput#buffer#get_text ())

Otherwise, it is viewed by a compiler as you're providing 3 arguments to prerr_endline .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM