in C++ I have macros like:
#ifdef DEBUG
#define dbgAssert(condition, message)/
if(!(condition)){ implementation.Assert(message); }
#else
#define dbgAssert(condition, message)
#endif
This approach is efficient as the condition is never tested if we're not in debug mode particularly when some of the conditions can be particularly heavy on CPU.
Is there a way to implement this type of one liners in Haxe?
This is a very simple example as there are some macros with a dozen conditional definitions (depending on multiple parameters) I can't efficiently maintain redundant redefinitions all over the place.
Here is a slightly more interesting system which allows me to always test for the simplest condition and add heavyer tests depending on the level:
#if 4 == ASSERT4LEVEL
#define lvlAssert4(conditionlvl1, conditionlvl2, conditionlvl3, conditionlvl4, message)/
myAssert((conditionlvl1) && (conditionlvl2) && (conditionlvl3) && (conditionlvl4), message)
#elif 3 == ASSERT4LEVEL
#define lvlAssert4(conditionlvl1, conditionlvl2, conditionlvl3, conditionlvl4, message)/
myAssert((conditionlvl1) && (conditionlvl2) && (conditionlvl3), message)
#elif 2 == ASSERT4LEVEL
#define lvlAssert4(conditionlvl1, conditionlvl2, conditionlvl3, conditionlvl4, message)/
myAssert((conditionlvl1) && (conditionlvl2), message)
#else
#define lvlAssert4(conditionlvl1, conditionlvl2, conditionlvl3, conditionlvl4, message)/
myAssert((conditionlvl1), message)
#endif
How can I replicate this behavior without executing the conditions?
Frabbits answer is the correct one for this situation, but here is the macro version as well.
@:macro static public function dbgAssert(condition, message) {
#if debug
return macro if (!($condition)) { implementation.Assert($message); }
#else
return macro {};
#end
}