Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Typing problems when using LablGTK
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-02-05 (16:57)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: Typing problems when using LablGTK
Hello Mattias,

> The first program works fine, but if I take the let button-part and
> move it to a separate function called mini, the compilation fails.
> The type of the method attach is different.
> What have I misunderstood?

Your problem is explained in the tutorial (1st part of the reference
manual) in section 2.1.3. To be correctly handled, labeled and
optionals arguments need to be known at the application point.  If you
move this application out of its context, the type information is
lost, and an incorrect type is inferred.

The standard solution is to add a type annotation on objects whose
methods have optional arguments:

let mini (table : GPack.table) =
  GButton.button ~label:"mini"
    ~packing:(table#attach ~left:3 ~top:3 ~expand:`BOTH) ();

> /mattias
> P.s. I learning GNOME using the book "Beginning GTK+/GNOME
> Programming", and sometimes it is difficult to map GTK in C to
> LablGTK. I am on page 111.

While the relation is kept intuitive, there are indeed differences
between GTK and LablGTK. Otherwise you wouldn't be able to write much
shorter programs :-) If you really don't know how to translate, the
low-level interface gives access to the raw calls, but this is much
less comfortable.

> Program 2, fails
> ================
> open GMain
> let mini table : unit =
>   GButton.button ~label:"mini"
>     ~packing:(table#attach ~left:3 ~top:3 ~expand:`BOTH) ();
>   ()
> let main () =
>   let window = GWindow.window ~border_width:10 ~title:"Fun with tables" ~width:320 ~height:200 () in
>   window#connect#destroy ~callback:Main.quit; 
>   let table = GPack.table ~rows:10 ~columns:10 ~packing:window#add () in
>   mini table; 
>   window#show ();
>   Main.main ()
> let _ = Printexc.print main ()