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: -- (:)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] How to avoid compiling some code (like #ifdef in C)
> --begin--
> open Printf
> let compile_code = false
> let a () = printf "toto\n"
> let _ = if compile_code then let t = 1 in a ()
> --end--
> In above code, if compile_code is true, then the auto-test is executed,
> otherwise not. But if compile_code is false, is the code corresponding
> to 'let t = 1 in a ()' generated?

With ocamlopt, that code is eliminated as part of constant

With ocamlc, the code of "let t = 1 in a()" is generated.

> Does anybody see a better approach to do such a thing?

Others mentioned preprocessing with the tool of your choice.  Camlp4
can do #ifdef-style conditional compilation, but in a pinch you can
even use cpp as your preprocessor.

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.

- Xavier Leroy

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