"Sputnik" help  
Sputnik Help
Language Reference - Datatypes - Enums

Enums

Enums are a variable that lets you get a number from it and each one is unique.

A local scope my enum is made like:

enum
{
    $Cat,
    $Dog,
    $Fox,
    $Cow,
    $Pig
}
say "Cat: " . $Cat; // Prints 0
say "Dog: " . $Dog; // Prints 1
say "Fox: " . $Fox; // Prints 2
say "Cow: " . $Cow; // Prints 3
say "Pig: " . $Pig; // Prints 4

Another local scope but this time with a few base variables an defining the local using the my scope to make all the variables be local only:

my enum
{
    $Cat,
    $Dog = 20,
    $Fox,
    $Cow = 55,
    $Pig
}
say "Cat: " . $Cat; // Prints 0
say "Dog: " . $Dog; // Prints 20
say "Fox: " . $Fox; // Prints 21
say "Cow: " . $Cow; // Prints 55
say "Pig: " . $Pig; // Prints 56

Same as above but with all variables set to global scope only:

global enum
{
    $Cat,
    $Dog = 20,
    $Fox,
    $Cow = 55,
    $Pig
}
say "Cat: " . $Cat; // Prints 0
say "Dog: " . $Dog; // Prints 20
say "Fox: " . $Fox; // Prints 21
say "Cow: " . $Cow; // Prints 55
say "Pig: " . $Pig; // Prints 56

Another way to do local scope using the "l" flag (the "l" flag just ensures the variables are local scope):

"l" enum
{
    $Cat,
    $Dog = 20,
    $Fox,
    $Cow = 55,
    $Pig
}
say "Cat: " . $Cat; // Prints 0
say "Dog: " . $Dog; // Prints 20
say "Fox: " . $Fox; // Prints 21
say "Cow: " . $Cow; // Prints 55
say "Pig: " . $Pig; // Prints 56

Global scope again but this time using the "g" flag like:

"g" enum
{
    $Cat,
    $Dog,
    $Fox,
    $Cow,
    $Pig
}
say "Cat: " . $Cat; // Prints 0
say "Dog: " . $Dog; // Prints 1
say "Fox: " . $Fox; // Prints 2
say "Cow: " . $Cow; // Prints 3
say "Pig: " . $Pig; // Prints 4
test();
function test()
{
    say "Cat: " . $Cat; // Prints 0
    say "Dog: " . $Dog; // Prints 1
    say "Fox: " . $Fox; // Prints 2
    say "Cow: " . $Cow; // Prints 3
    say "Pig: " . $Pig; // Prints 4
}

Note another possible flag is the "a" flag which means *any* meaning it will grab/override either a local or global depending on what exists however in *any* mode it will create locals if no globals exists.

So as you can see there are multiple ways to create a global/local enum either using flags or keywords. However I recommend using the keyword for readability.

However most of the time enums will be used globally in this style:

enum Countries
{
    $England,
    $France,
    $Germany,
    $Netherlands,
    $Portugal = 50,
    $Italy,
    $Russia
}
say "England: " . Countries->$England; // Prints 0
say "France: " . Countries->$France; // Prints 1
say "Germany: " . Countries->$Germany; // Prints 2
say "Netherlands: " . Countries->$Netherlands; // Prints 3
say "Portugal: " . Countries->$Portugal; // Prints 50
say "Italy: " . Countries->$Italy; // Prints 51
say "Russia: " . Countries->$Russia; // Prints 52

You can also create enums using specified addition numbers instead of single ++ incremental decimals by using the flags 'i' rule example:

"i:2" enum Countries
{
    $None,
    $England,
    $France,
    $Germany,
    $Netherlands,
    $Portugal,
    $Italy,
    $Russia
}
say "England: " . Countries->$England; // Prints 2
say "France: " . Countries->$France; // Prints 4
say "Germany: " . Countries->$Germany; // Prints 6
say "Netherlands: " . Countries->$Netherlands; // Prints 8
say "Portugal: " . Countries->$Portugal; // Prints 10
say "Italy: " . Countries->$Italy; // Prints 12
say "Russia: " . Countries->$Russia; // Prints 14

You set how much you want the increment to be by the number after i: in the options of the enum.

You can also create enums using bitwise flag numbers by using the flags 'f' rule example:

"f" enum Countries
{
    $None,
    $England,
    $France,
    $Germany,
    $Netherlands,
    $Portugal,
    $Italy,
    $Russia
}
say "England: " . Countries->$England; // Prints 1
say "France: " . Countries->$France; // Prints 2
say "Germany: " . Countries->$Germany; // Prints 4
say "Netherlands: " . Countries->$Netherlands; // Prints 8
say "Portugal: " . Countries->$Portugal; // Prints 16
say "Italy: " . Countries->$Italy; // Prints 32
say "Russia: " . Countries->$Russia; // Prints 64

You can still use defaults with flags example:

"f" enum Countries
{
    $None,
    $England,
    $France,
    $Germany,
    $Netherlands,
    $Portugal,
    $Italy,
    $Russia
} 
say "NONE: " . Countries->$None; // Prints 0
say "England: " . Countries->$England; // Prints 1
say "France: " . Countries->$France; // Prints 2
say "Germany: " . Countries->$Germany; // Prints 4
say "Netherlands: " . Countries->$Netherlands; // Prints 8
say "Portugal: " . Countries->$Portugal; // Prints 16
say "Italy: " . Countries->$Italy; // Prints 32
say "Russia: " . Countries->$Russia; // Prints 64
You can also combine flags.

Warning - You must have already defined the flags you wish to combine! You can't combine flags that don't exist in the enum yet.

"f" enum Countries
{
    //$Test = $England + $France IT WOULD NOT WORK HERE
    $England,
    $France,
    //$Test = $England + $France IT COULD WORK HERE
    $Germany,
    $Netherlands,
    $Portugal,
    $Italy,
    $Russia,
    $Test = $England | $France // | is the correct operator to combine flags
} 
say "England: " . Countries->$England; // Prints 0
say "France: " . Countries->$France; // Prints 1
say "Germany: " . Countries->$Germany; // Prints 2
say "Netherlands: " . Countries->$Netherlands; // Prints 4
say "Portugal: " . Countries->$Portugal; // Prints 8
say "Italy: " . Countries->$Italy; // Prints 16
say "Russia: " . Countries->$Russia; // Prints 32
say "Test: " . Countries->$Test; // Prints 1

You can also define an enum as an override that will replace an existing enum with the same name example:

enum Cat
{
    $A,
    $B
}
 
"r" enum Cat
{
    $A,
    $B,
    $C
}
 
say Cat->$C; // Prints 2

You can also embed an enum inside a { } block like:

// Check if the enum already exists and
// if it does already exists don't create it
if (!EnumExists("Animals"))
{
    // Create the enum using the Embedded keyword
    Embedded enum Animals
    {
        $Dog,
        $Cat,
        $Fox
    }; // note ; is required here
}
say Animals->$Dog;
say Animals->$Cat;
say Animals->$Fox;
// PRINTS
// 0
// 1
// 2

 


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