Are there DAML pre-compiler conditions

G-d willing

Hello,
I would like to control the debug statements in my code using a precompiled condition. So, it will be turned on in the dev environment, and in production, it will be off.
So, eventually, I will have something like

{-# PrecompilerCondition = True  #-}

-- Some DAML code

{-# IF PrecompilerCondition #-}
debug "Hello"
{-# ENDIF PrecompilerCondition #-}

Thanks,
Avraham

Hello @cohen.avraham,

Daml’s GHC foundations mean that it inherits GHC’s support for CPP, so the code you want would look something like this

{-# LANGUAGE CPP #-}

-- the usual style for CPP macros is SNAKE_CASE_ALL_CAPS
#define PRECOMPILER_CONDITION 

module Example where

import Daml.Script

test : Script ()
test = do
#ifdef PRECOMPILER_CONDITION
  debug "Hello"
#endif
  pure ()

It is also possible to define CPP macros in flags to daml commands instead of defining them within the daml source files, e.g.

daml build --ghc-option -DPRECOMPILER_CONDITION, but note that you will also need to define the macros in subsequent invocations of e.g. daml test, so daml test --ghc-option -DPRECOMPILER_CONDITION.

For a third option, you can add CPP macro definitions in your project’s daml.yaml, under build-options:

# daml.yaml
sdk-version: 2.8.3
name: example
version: 0.0.1
source: daml
dependencies:
- daml-prim
- daml-stdlib
- daml-script
build-options: [--ghc-option=-DPRECOMPILER_CONDITION]

Note, however, that there’s currently a bug (daml#17821) in our IDE that prevents it from working properly on projects with CPP pragmas. Working through the command line with daml build and daml test should be fine, however.

4 Likes