Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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
#endif
  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
    ~callback:single_step_ev_sim;
#endif

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:

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

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.

Rich.

-- 
Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj
Merjis Ltd. http://www.merjis.com/ - 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. http://www.annexia.org/freeware/makeplus/

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners