"Sputnik" help  
Sputnik Help
Language Reference - Variables - Declaring Constants

Declaring Constants

Constants are declared and created using Const keyword like:

// define a constant
const $var1 = 1;
$var1 = 10; // cannot change a constant
say $var1; // prints 1

By default all constants are global on creation however you can set the global keyword to make it more obvious what you intend:

// define a constant
global const $var1 = 1;
$var1 = 10; // cannot change a constant
say $var1; // prints 1

More examples:

Const $a = "Hello";
say $a; // Prints: Hello
$a = 10; // Cant change it
say $a; // Prints: Hello
unset($a); // Cant even delete it!
say $a; // Prints: Hello
// BUT you can RE define it
// This is because the only way
// to change a constant variable
// is to TOTALLY replace it by a NEW
// constant variable
Const $a = "Cat";
say $a; // Prints: Cat
This way you can use Const to create a variable and you can feel safe knowing it's not going to suddenly change by accident unless for some reason Const is called again with the same variable name.

However since your Constants should only be called once it should not be possible to get two of them.

You can also create local constants and only exist within the scope they are inside (but do not persist after its destroyed) like so:

Function Cat()
    my const $meows = 10;
    $meows = 6;
    say $meows;

// 10
This type of constant is useful if you want to make sure you dont accidently change its value within the function but dont want it to exist outside of the functoin.

Alternatively you could take a look at the Preprocessor to simulate global constants like this:

#define VAL 777
say VAL;
// 777

You can also define a @macro that does not change as well:

MacroAdd("Kitten", "Meow");

say @Kitten;
@Kitten = "Felix";
say @Kitten;
// Meow
// Meow


Cheryl (uberfox@hotmail.com)