If defined

Purpose: Conditional compilation.

if-defined <symbol>
    <any code>
end-defined

if-defined <symbol> [ equal | not-equal | lesser-than | lesser-equal | greater-than | greater-equal ] <number>
    <any code>
end-defined

if-not-defined <symbol>
    <any code>
end-defined

Without a comparison clause, if-defined will cause <any code> to be compiled if <symbol> is defined (see "--cflag" option in gg); if <symbol> is not defined, then <any code> is not compiled.

With a comparison clause, if-defined will cause <any code> to be compiled if <symbol> is equal/not equal/lesser than/lesser or equal/greater than/greater or equal than <number> constant, depending on the clause used (see "--cflag" option in gg). If <symbol> is not defined, then its value is assumed to be 0, and <any code> may still be compiled if the comparison clause and the <number> are such that the condition is true for <symbol> being 0.

if-not-defined will cause <any code> to be compiled if <symbol> is not defined (see "--cflag" option in gg); if <symbol> is defined, then <any code> is not compiled.

Note that <symbol> can start with an underscore, unlike Golf variable names; this is because defined symbols aren't variables and generally are set by the outside environment or during compilation (see "--cflag" option in gg).
Examples
The following code will have a different output depending on how is the application compiled:
if-defined DEF1
    @Defined
end-defined
if-not-defined DEF1
    @Not defined
end-defined
if-defined DEF2 equal 10
    @DEF2 is 10
end-defined
if-defined DEF2 equal 0
    @DEF2 is 0
end-defined

If compiled with:
gg -q

then the output is:
Not defined
DEF2 is 0

If compiled with:
gg -q --cflag="-DDEF1 -DDEF2=10"

then the output is:
Defined
DEF2 is 10

See also
Program flow
break-loop  
call-handler  
code-blocks  
continue-loop  
do-once  
exit-handler  
if-defined  
if-true  
quit-process  
return-handler  
start-loop  
See all
documentation


Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.