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
[Caml-list] How to avoid compiling some code (like #ifdef in C)
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-09-11 (16:28)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] How to avoid compiling some code (like #ifdef in C)
On Thu, Sep 11, 2003 at 05:30:56PM +0200, Xavier Leroy wrote:
> Whether this is a "better" approach is open to debate.  With a
> preprocessor, you can remove not just code for expressions, but also
> other kind of code, e.g. code for the toplevel binding "let a() = ...".
> So, you get more flexibility, but at the expense of less static
> checking.  In particular, code that is #ifdef-ed out is not
> type-checked, while it is type-checked in your solution above.

However one place where the cpp approach is very useful (and better)
is code such as this:

  let simulation_menu = factory#add_submenu "Simulation" in
#ifdef DEBUG
  let debug_menu = factory#add_submenu "Debug" in
  let help_menu = factory#add_submenu "Help" in


#ifdef DEBUG
  let factory = new GMenu.factory debug_menu ~accel_group in
  factory#add_item "Single Step (1 Event)" ~key:GdkKeysyms._T

I haven't found a satisfactory alternative to the above code which
doesn't cause possible runtime errors. eg. One alternative is:

  let debug_menu = if debug then Some (...) else None in


  match debug_menu with
      None -> ()
    | Some menu ->
      (* code to populate the menu *)

but this is not just tedious in real life, but also could have bugs
only discovered at runtime.

I'm open to a better way to do this ...

Another important case in our program:

let icon icon_type size =
  let icon = GMisc.image () in
  icon#set_stock icon_type ~size:size;
#else (* LABLGTK from CVS: *)
let icon icon_type size =
  let icon = GMisc.image () in
  icon#set_stock icon_type;
  icon#set_icon_size size;

These sorts of API changes are *really* hard to work around without a
preprocessor, unless you have two separate modules and play tricks in
the Makefile.

Again, I'm open to suggestions as to how to do this better.


Richard Jones.
Merjis Ltd. - all your business data are belong to you.
MAKE+ is a sane replacement for GNU autoconf/automake. One script compiles,
RPMs, pkgs etc. Linux, BSD, Solaris.

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: