English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
question about how to bind c++ classes to ocaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2006-08-14 (08:45)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] question about how to bind c++ classes to ocaml
From: "Michael Wohlwend" <micha-1@fantasymail.de>

> > Calls from ocaml to C are very cheap. If your access function doesn't
> > do any allocation (i.e. never calls the GC), you can even make it
> > faster by marking it as "noalloc". Beware many functions do
> > allocate, including copy_double or copy_string.
> thanks for pointing this out. The "noalloc"-thing isn't really
> described in the docu? :-)

I suppose this is because it is a bit tricky. You have to look at all
the dependencies to be sure that the function will never trigger the
GC, and you might easily get it wrong.

> Another question which came up was: you can only declare normal
> functions (not methods) as "external", so you have to write a c
> function for every c++ member function. Do you rebuild the
> inheritance hierachy somehow through the type system (maybe with
> those mysterious phantom types?) or just put an ocaml class system
> on top of it? 

My approach in lablgtk was to first use phantom types, ie use
parameters in abstract types to simulate subtyping. Then there is a
second layer using ocaml objects, but I'm not sure you need it in
general. It isonly needed because GTK+ has so many classes that it is
difficult to track which function comes from which class.

In Labltk for instance, there are only phantom types, and without
subtyping (it uses a coercion function "coe" instead). This works well
if you have a hierarchy with few layers (only two in labltk)


Jacques Garrigue