Conditional compilation
Posted: Wed May 23, 2012 5:19 am
I've not used the @help directive ( Noted here and originally discussed and announced here) very much - it doesnt match how I work or lookup stuff but the syntax/concept is interesting and could be extended to a couple of other convenient things.
1. Debugging
Specify some code to be included/executed (displayed) if debug is enabled when the compiler is run
e.g.
If -d compiler flag is specified when the code is compiled, the expressions and statements prefixed by @debug will be compiled into the generated exe.
If debug is not specified the prefixed code will be ignored.
( the above example duplicates trace ( sort-of, without some gingerbread) but it doesn't need to be limited to only display
2. Platform specific code
Prefix lines with a tag indicating the following code is only generated specific to a particular platform/back-end
simple e.g
in the above, the writeline line is only compiled/code generated for a backend .Net/CLR build,
the println line is similarly only included for a backend jvm build
( the -other is a catch all if nothing else is previously matched)
This comes from modifying the cobra test code to work with a java build/back-end compile
- In many cases the test code uses/relies on platform library calls (mostly peripheral to what the tests are verifying).
The current resolution is either split the file into platform dependent and independent parts or duplicate the file - change the platform dependent calls for the platform and tag (compiler directive) the files so that they are only compiled if the back-end compile matches the tag setting.
The above directives gives a more granular approach to some form of multi-platform support ( as a interim step before a full platform independent library exists).
FWIW I think the D language has a similar construct for specifying conditional compilation D Conditional Compilation
1. Debugging
Specify some code to be included/executed (displayed) if debug is enabled when the compiler is run
e.g.
result = .aVeryBigCalc(a1, a2, a3)
@debug print .prettyFormat(result)
... # do something with result
If -d compiler flag is specified when the code is compiled, the expressions and statements prefixed by @debug will be compiled into the generated exe.
If debug is not specified the prefixed code will be ignored.
( the above example duplicates trace ( sort-of, without some gingerbread) but it doesn't need to be limited to only display
2. Platform specific code
Prefix lines with a tag indicating the following code is only generated specific to a particular platform/back-end
simple e.g
# platform varying Hello world
namespace Test
class Test
def main is shared
@platform-clr Console.writeLine('Hello, world.')
@platform-jvm System.out.println('Hello, world.')
@platform-other assert false, 'Need platform display output call'
in the above, the writeline line is only compiled/code generated for a backend .Net/CLR build,
the println line is similarly only included for a backend jvm build
( the -other is a catch all if nothing else is previously matched)
This comes from modifying the cobra test code to work with a java build/back-end compile
- In many cases the test code uses/relies on platform library calls (mostly peripheral to what the tests are verifying).
The current resolution is either split the file into platform dependent and independent parts or duplicate the file - change the platform dependent calls for the platform and tag (compiler directive) the files so that they are only compiled if the back-end compile matches the tag setting.
The above directives gives a more granular approach to some form of multi-platform support ( as a interim step before a full platform independent library exists).
FWIW I think the D language has a similar construct for specifying conditional compilation D Conditional Compilation