How to read this OCaml type signature?

I am currently experimenting using OCaml and GTK (using lablgtk bindings). However, the documentation is not the best, and although I can decide how to use most of the features, I am stuck with changing notebook pages (switching to another tab).

I found a function that I need to use , but I do not know how to use it. The documentation seems to suggest that it is in the GtkPackProps.Notebook submodule, but I don't know what to call it.

In addition, this function has a type signature different from the one I saw before.

  val switch_page : ([> `notebook ], Gpointer.boxed option -> int -> unit) GtkSignal.t 

I think it returns GtkSignal.t , but I donโ€™t know how to pass the first parameter to the function (the whole part is in brackets).

Does anyone have a code example showing how to change a laptop page, or maybe give me some tips on how to do this?

+4
source share
2 answers

What you find is not a function, but a signal. The functional type that you see in your type is the callback type that will be called when the page switches, but does not call it.

since the page type switch_ is read as: signal ( GtkSignal.t ) raised by notebook [> `notebook ] , whose callbacks are of type Gpointer.boxed option -> int -> unit

Generally speaking, with lablgtk you better stay away from the low-level modules of Gtk * and use the higher-level module tge G [AZ]. These APIs are similar to C Gtk, and I always use the main Gtk document to help myself.

In your case, you want to use the GPack.notebook object and its goto_page method.

+5
source

You have found the polymorphic variant , which is described in the manual in section 4.2, and input rules always puzzle. I believe the signature says that the switch_page function expects as an argument a GtkSignal.t , which is an abstraction parameterized by two types:

  • First type parameter

     [> `notebook] 

    includes as values โ€‹โ€‹any polymorphic variant, including notebook (that which is more than a means).

  • The second type is a regular function.

If I read the documentation for GtkSignal.t , this is not a function at all; This is a record with three fields:

  • name is a string.
  • classe is a polymorphic option that can be a โ€œnotebookโ€ or something else.
  • marshaller is a marshaller for the function type Gpointer.boxed option -> int -> unit .

Hope this helps. If you have more problems, section 4.2 of the polymorphic variation guide can help you figure it out.

+5
source

Source: https://habr.com/ru/post/1315592/


All Articles