"Sputnik" help  
Sputnik Help
defined

Description

The defined function in Sputnik's preprocessor serves the purpose of checking whether a specified symbol is defined during the compile-time preprocessing phase. It offers a straightforward and efficient way to conditionally include or exclude code based on the existence of a particular symbol.

Parameters

<symbol> The symbol whose existence is to be checked. It can be any valid identifier (macro etc).

Return Value

Success: Returns (true) 1 if the specified symbol is defined.
Failure: Returns (false) 0 if the specified symbol is not defined.

Remarks

The defined function doesn't require parentheses and can be used directly in conditions.

It is particularly useful for creating versatile and adaptive code that adjusts its behavior based on the compilation environment.

Related

Example (#)

The special function defined is used in ‘#if’ and ‘#elif’ etc expressions to test whether a certain name is defined as a macro. defined name and defined (name) are both expressions whose value is 1 if name is defined as a macro at the current point in the program, and 0 otherwise. Thus, #if defined MACRO is precisely equivalent to #ifdef MACRO.

Defined is useful when you wish to test more than one macro for existence at once. For example,

#if defined (__CAT__) || defined (__DOG__)
Would succeed if either of the names __CAT__ or __DOG__ is defined as a macro.

Conditionals written like this:

#if defined BUFSIZE && BUFSIZE >= 1024

Can generally be simplified to just

#if BUFSIZE >= 1024
Since if BUFSIZE is not defined, it will be interpreted as having the value zero.

If the defined operator appears as a result of a macro expansion then it will not be evaluated.

Using the defined function:

#if defined(foo)
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is false

#define foo

#if defined(foo)
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is true

Using the defined function with parentheses:

#if defined foo
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is false

#define foo

#if defined foo
    say "Condition is true";
#else
    say "Condition is false";
#endif
// PRINTS
// Condition is true

Of course defined can be combined with normal conditions:

#define OPTION 1

#if OPTION == 1 && defined(foo)
    say "Option is 1 with foo";
#elif OPTION == 1 && !defined(foo)
    say "Option is 1 without foo";
#else
    say "Option is neither 1 nor 2";
#endif
// PRINTS
// Option is 1 without foo

#define foo

#if OPTION == 1 && defined(foo)
    say "Option is 1 with foo";
#elif OPTION == 1 && !defined(foo)
    say "Option is 1 without foo";
#else
    say "Option is neither 1 nor 2";
#endif
// PRINTS
// Option is 1 with foo

 

Example (.)

Example of usage:

#macro foo()
    .if defined(bar)
        say "Condition is true";
    .else
        say "Condition is false";
    .endif
#endm

foo();
#define bar
foo();

// PRINTS
// Condition is false
// Condition is true

Larger example:

#macro foo($cat)
    local $fox
    local $dog = 7
    localm kitten()^
        say "testy";
    ^
    // not possible since an argument isnt a macro!
    //.if defined($cat)
    //    say "Condition is true";
    //.else
    //    say "Condition is false";
    //.endif
    .if defined($fox)
        say "Condition is true";
    .else
        say "Condition is false";
    .endif
    .if defined($dog)
        say "Condition is true";
    .else
        say "Condition is false";
    .endif
    .if defined(kitten)
        say "Condition is true";
    .else
        say "Condition is false";
    .endif
    .if defined($YAY)
        say "Condition is true";
    .else
        say "Condition is false";
    .endif
#endm

foo("meow");

// PRINTS
// Condition is true
// Condition is true
// Condition is true
// Condition is false
Notice it can be used to check locals and local macros etc but not arguments.

Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org