Wiki
Version 3 (modified by Charles, 11 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.

Conditions

  • true
  • false
  • comparison (== <> < > <= >=)
  • E1 and E2
  • E1 or E2
  • not E
  • String literal
  • ( E )
  • .METHOD(ARGS)
  • Bare Symbol

Possible methods:

  • .defined('FOO')
  • .environ('FOO')
  • .definedOrEnviron('FOO')
  • .cobraVer
  • .platform
  • .opSys

Open Questions

  • Could methods return various kinds of objects instead of just strings? For example, a Version object that does the right thing for comparisons.

Implementation

There is no implementation at this time. If we implement this, it's unlikely that all of it will be implemented at once. However, it would be ideal to complete the design and then to implement a strict subset so that future additions to the language do not break any code.

See Also