GOLF(2gg)

πŸ“¦ version 601.4.41, in ubuntu questing

πŸ’– GOLF(2gg) β€” the if-defined glow-up πŸ’–

Development Β· Golf manual, section 2gg Β· bestie edition πŸŽ€


✨ NAME ✨

if-defined β€” conditional compilation, but make it a vibe check for your code πŸ¦‹ (it lives in the program-flow family)


🎯 PURPOSE 🎯

Conditional compilation. That's it, that's the whole job. Some code gets compiled, some code does NOT, and the compiler decides based on what's defined. Main character energy for your build system. πŸ’…


πŸ“ SYNTAX πŸ“

Okay so there are three whole shapes this can take, and they all get closed off with end-defined:

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

πŸ’… DESCRIPTION πŸ’…

πŸ“ no comparison clause

Without a comparison clause, if-defined makes <any code> get compiled if <symbol> is defined (peep the --cflag option in gg for how those symbols get set up in the first place). And if <symbol> is not defined? Then <any code> just doesn't get compiled. No notes. πŸ™ˆ

πŸ”₯ with a comparison clause

With a comparison clause, if-defined compiles <any code> when <symbol> is equal / not equal / lesser than / lesser or equal / greater than / greater or equal than the <number> constant β€” whichever clause you actually used, obviously (again, --cflag option in gg).

Now here's the ✨ plot twist ✨: if <symbol> is not defined, its value is assumed to be 0. So <any code> can still get compiled if the comparison clause and the <number> are set up such that the condition is true for <symbol> being 0. Sneaky? A little. Iconic? Absolutely. πŸ’€

🧚 if-not-defined

if-not-defined compiles <any code> if <symbol> is not defined (yes, --cflag option in gg, you know the drill). And if <symbol> is defined? Then <any code> is not compiled. Periodt.

🌸 Note bestie: <symbol> is allowed to start with an underscore, which Golf variable names are NOT allowed to do. Why? Because defined symbols aren't variables at all β€” they're generally set by the outside environment or during compilation (see the --cflag option in gg). Different species entirely, iykyk. πŸ’œ

🌈 EXAMPLES 🌈

The following code will have a different output depending on how the application is 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?? Same code, two completely different lives. She contains multitudes. πŸ¦‹βœ¨


πŸ‘€ SEE ALSO πŸ‘€

Program flow β€” go read it, it's giving essential context:

  • 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 πŸ“š


That's the whole page, bestie 🫢 this was the $VERSION of GOLF(2gg) dated $DATE β€” go compile something cute πŸ’–πŸŽ€

πŸ“¦ where this page comes from