Static ifs are used such that at the flag's notice, it can change the behavior of your code.
For example, take Debug mode. You have functions that tell you what is happening as of the execution of certain parts of your code. When you disable debug mode, those messages do not appear. When enabled, those messages appear.
Yes they are.This confused me a bit... are you saying they are executed during compilation time?
Yes they are.
In other words. If the condition is not met, the code inside it is ignored by the compiler.
function foo takes nothing returns nothing
static if DEBUG_MODE then
call BJDebugMsg("Killing triggering unit")
endif
// If debug mode is true, the message will display.
call KillUnit(GetTriggerUnit())
endfunction
What are the differential characters of compile-time ifs (static ifs if you prefer) on one hand, and runtime ifs on the other?
(while I think I have a general idea of the meaning of the compile time vs runtime difference when it comes to errors, I'm not able to figure out precisely what the implications for if statements might be)
In other words, could you please explain in simple terms when is using static ifs advantageous over the usual ifs?
And conversely, could you name an example situation when you do NOT want to use static ifs?
Would this example I just thought at be a case where using static if would be appropriate?
static if: - a constant that I suspect is already defined a compile time exists -
then: - use the predefined constant -
else: - explicitly define a guess-value for the nonexisting constant, and use the guessed value for the rest of the code
static if(conditions)
compile code
if (condition)
logic & actions
//This is a bizarre mixture between C & JASS syntax and some random custom simplified library, look the other way.
function ButtonDown takes nothing returns boolean
static if (OS == Windows) //Static if for compilation
if (Windows.mouse.position == positionOfButton) and (Windows.mouse.button1.buttonDown == true) //normal if for logic
return true
end if
else if (OS == Android) // part of static if for compilations
if (Android.screenTap.position == positionOfButton) //regular if for logic
return true
end if
end if
return false
end function
function ButtonDown takes nothing returns boolean
if (Windows.mouse.position == positionOfButton) and (Windows.mouse.button1.buttonDown == true) //Logical if
return true
end if
return false
end function
function ButtonDown takes nothing returns boolean
if (OS == Windows)
if (Windows.mouse.position == positionOfButton) and (Windows.mouse.button1.buttonDown == true)
return true
end if
else if (OS == Android)
if (Android.screenTap.position = positionOfButton)
return true
end if
end if
return false
end function
static if LIBRARY_UnitIndexer then
do_some_indexer_things()
else
do_some_not_indexer_things()
endif
static if not LIBRARY_TimerUtils then
set tmr = CreateTimer()
set table[GetHandleId(tmr)] = this
else
set tmr = NewTimerEx(this)
endif
NewTimerEx())
), and if ithe library doesn't exist the code line creates a new one.