caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] How to avoid compiling some code (like #ifdef in C)
Date: Thu, 11 Sep 2003 17:28:10 +0100	[thread overview]
Message-ID: <20030911162810.GA27415@redhat.com> (raw)
In-Reply-To: <20030911173056.A29749@pauillac.inria.fr>

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


  reply	other threads:[~2003-09-11 16:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-10 17:14 David MENTRE
2003-09-10 17:31 ` Markus Mottl
2003-09-11 15:30 ` Xavier Leroy
2003-09-11 16:28   ` Richard Jones [this message]
2003-09-12 12:15   ` David MENTRE

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030911162810.GA27415@redhat.com \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).