Variables
Constants and variables associate a name with a value of a particular type. The value of a constant can't be changed once it's set, whereas a variable can be set to a different value in the future.
No Identifier Shadowing
Variable identifiers are never allowed to shadow the identifiers from outer scope:
foo := 1;
{
foo := 2; //compile error
}
If you can confirm that you really need name shadowing(why?), you can annotate the variable with @shadowing
:
foo := 1;
{
foo @shadowing := 2; //good
}
Or you can just turn the compile-time safety check off by using the @compileTimeSafetyProfile()
(but again, why you need this?):
foo := 1;
@compileTimeSafetyProfile(safety.compiletime.ruleset.no_shadowing, false);
{
foo @shadowing := 2; //good
}
Long Identifiers
Normal identifiers can only contains numbers, characters, and underscores. You can make the identifier to be regular string by using @""
s to quote them:
@"hey i am a variable with a very long identifier!" := "Hello";
The long identifiers are also allowed to be the same as keywords:
@"true" := true;
assert(@"true" == true);
Variables and Constants
You can specify the mutability of variables by adding a const/mutable
specifier to it, you can't modify variable that is const
:
foo := 1;
//equals to:
foo const := 1;
foo = 2; //error! try to assign a constant
bar mutable := 2;
bar = 42; //good
const/mutable
will be part of the variable's type:
foo const := 1:i32;
bar mutable := 2:i32;
//equals to:
foo : const i32 := 1;
bar : mutable i32 := 1;
//or
bar : i32 := 1;
You can remove the const
in type by type traits:
foo : const i32 = 1;
BarType := foo~getType()~removeConst();
bar : BarType = 2;
bar = 42; //good
Storage Duration Specifiers
Global Storage Duration Specifier
The global variables have static lifetime and order-independent. Their initializer are implicitly compile-time. The variables declares in the module level are implicitly global.
Static Storage Duration Specifier
The static variables also have static lifetime and order-independent. Their initializer are implicitly compile-time. Only the variables declares in the block level can be static.