Wiki
Version 2 (modified by Charles, 12 years ago)

--

If Directive

This does not describe an existing feature. It's place to jot down notes for my idea of an @if directive and related features.

Motivation

Conditional inclusion of source code (aka "conditional compilation") is needed in some circumstances. Some of the better known ones are:

  • Adjusting for the back-end (CLR, JVM)
  • Demo vs. Production
  • Debug or not
  • Adjusting for compiler options (-include-tests, etc.)

Other languages including C# (#if, #define) and D (static if, version, ...) have condition compilation features which are taken advantage of by their communities.

Syntax

General, full form:

@if <condition>
   <code>
@else if <condition>
   <code>
@else
   <code>

But "@else if" and "@else" are optional and "@else if" can be repeated.

Defining Symbols

cobra -define:FOO -define:FOO=bar ...

Or in code:

@define FOO
@define FOO = 'bar'

Automatic Symbols

CLR, CLR20, CLR40, JVM, JVM16, JVM17, JVM18, OBJC, DOTNET, MONO, WINDOWS, UNIX, MAC, ASSERTS, TESTS, CONTRACTS, DEBUG, DST, ERT

... or we could go all lower case:

clr, jvm, objc, dotnet, etc.

See Also