From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 8FE66BDCB for ; Wed, 7 Sep 2005 14:03:10 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j87C3AxJ015676 for ; Wed, 7 Sep 2005 14:03:10 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA21061 for ; Wed, 7 Sep 2005 14:03:09 +0200 (MET DST) Received: from cirrus.purplecloud.com (cirrus.purplecloud.com [217.158.94.220]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j87C3830015671 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Wed, 7 Sep 2005 14:03:09 +0200 Received: (qmail 43905 invoked from network); 7 Sep 2005 13:03:08 +0100 Received: from 81-86-133-45.dsl.pipex.com (HELO ?192.168.1.5?) (81.86.133.45) by smtp.purplecloud.net with SMTP; 7 Sep 2005 13:03:08 +0100 Message-ID: <431ED6FB.9010009@jollys.org> Date: Wed, 07 Sep 2005 13:03:07 +0100 From: Peter Jolly User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Dmitry Bely Cc: caml-list Subject: Re: [Caml-list] camlp4 pa_macro References: In-Reply-To: Content-Type: text/plain; charset=ISO-2022-JP Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 431ED6FE.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 431ED6FC.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 ifdef:01 ndebug:01 expr:01 expr:01 printf:01 printf:01 ndebug:01 debugging:01 cmo:01 cmo:01 syntax:01 ifdef:01 kprintf:01 compiler:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 Dmitry Bely wrote: > 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. > 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 . 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 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.