Browse thread
camlp4 pa_macro
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2005-09-08 (08:43) |
From: | Dmitry Bely <dbely@m...> |
Subject: | Re: [Caml-list] camlp4 pa_macro |
Peter Jolly <peter@jollys.org> writes: >> Is it possible to achieve with pa_macro something like this: >> >> IFDEF NDEBUG THEN >> DEFINE LOG(expr) = () >> ELSE >> DEFINE LOG(expr) = Printf.printf expr >> ENDIF >> ... >> LOG("x=%d,y=%d" x y); >> >> Unfortunately the code above does not work: debug version is OK, but then >> NDEBUG is turned on I have >> >> "This expression is not a function, it cannot be applied" on LOG() >> expression. > > Getting camlp4 to pretty-print the code after macro expansion is a > useful technique for debugging this sort of problem: > > $ camlp4 pa_o.cmo pa_op.cmo pr_o.cmo pa_macro.cmo test.ml > ... > Printf.printf ("x=%d,y=%d" x y) > > It should be clear why that isn't working. I see. Thank you very much for the info. > >> If I use >> >> LOG "x=%d,y=%d" x y; >> >> then the release version surprisingly works, but the debug one gives >> >> "Parse error: currified constructor" > > Yes, because this does not pass any arguments to the LOG macro - it > expands it with an empty <expr>. So this works in the latter case, > because LOG just expands to "Printf.printf", but in the former case you > end up with > > () "x=%d,y=%d" x y > > which is a syntax error as reported. > >> How to overcome this? > > IFDEF NDEBUG THEN > DEFINE LOG = Printf.kprintf ignore > ELSE > DEFINE LOG = Printf.printf > ENDIF It's quite useless - let debug = ref true let log fmt = if !debug then Printf.kprintf print_string fmt else Printf.kprintf ignore fmt will in fact give the same result. It does not solve the initial problem - completely remove the debugging code from the release binary. > Or just replace all instances of LOG with "if debug then Printf.printf", > on the grounds that the compiler is probably clever enough to prune > conditions that always evaluate to false, and you probably won't notice > any significant difference in speed even if it isn't. But the debugging code (format strings etc.) will be there. OK, the best I can get now is IFDEF NDEBUG THEN DEFINE LOG(expr) = () ELSE let dprintf = Printf.printf DEFINE LOG(expr) = expr ENDIF ... LOG(dprintf "x=%d,y=%d" x y); Not very elegant, but works. - Dmitry Bely