Variables can be declared in different contexts: Inside entities or classes (class or instance variables), inside functions (local variables), or inside certain control structures, such as for loops (also local variables). All variable declarations follow the following syntax, however:
var <variableName> [modifiers] [as <datatype>] [customModifiers] [= [assert] <initializer>]
Variable Name
By convention, the variable names in Sling follow the camelcase naming convention with the first letter of the variable name in lowercase and all subsequent words starting with an uppercase character:
// Variable names that follow the convention
validVariableName
anotherValidVariableName
// Variable names that do NOT follow the convention
ThisIsNotGood
this_is_also_not_good
THIS_IS_NOT_GOOD
ANDTHISISNOTGOOD
Here are sample variable declarations:
var integerVariable as int
var doubleVariable as double
var stringVariable as string
Modifiers for Local Variables
Valid modifiers for declaration of local variables are the following. Note that these modifiers act primarily as "hints" to the underlying platform, which may use them to modify the variables accordingly during execution. If the underlying platform does not support the functionality of the modifier, then the modifier will silently be ignored.
volatile (indicates "volatile"; what exactly this means depends fully on the underlying platform, the modifier is simply passed on directly)
final (the value of the variable must not be changed after initialization)
const (the variable represents a constant value that does not change)
synchronized (access to the variable will be conducted in a thread-safe manner)
Modifiers for Entity Variables
Valid modifiers for variables defined in an entity scope (eg. class, interface):
public (the variable can be accessed also from outside of the current entity)
private (only the current entity can access the variable)
protected (only the current entity and any entity that extends the current entity is able to access the variable)
nsprivate (only entities in the same namespace as the current entity are able to access the variable)
static (the variable is static, ie. is accessed as a member of the class, not as member of individual objects; the value of the variable is shared across all instances)
extern (this variable is just a reference to a variable that is actually defined and allocated elsewhere)
final (the value of the variable cannot change after initialization)
In addition, the following modifiers are also accepted, but are not processed or interpreted in Sling. Rather, their presence or lack of will be passed directly to the underlying platform / runtime environment: volatile, transient, event, synchronized, deprecated, property
Unless specified otherwise, entity variables are private.
Assertions
An assertion can be inserted after the equals sign `=' to validate the initial value. See the discussion on assertions under the "control structures" topic.
Initializer
If an equal sign `=' is present in the variable declaration, the initializer that follows it will be used to set the initial value of the variable. If type inference is invoked (see below), the initializer will also be used to determine the variable value.
Type Inference
Type Inference is a special feature in variable declarations, where the data type of the variable does not need to be specifically declared by the programmer, but will be determined automatically based on the content assigned to the variable. This can be achieved by simply omitting the "as" portion of the declaration, and assigning a value of a known data type instead:
// Automatically determined "as int", since 10 is an integer
var integerVariable = 10
// Automatically determined "as double", since 10.0 is a double
var doubleVariable = 10.0
// Automatically determined "as string", since assigning a string
var myString = "Hello World"
NOTE: If the type can be inferred from the initializer value, it is still allowed that the data type is also explicitly specified. However, if the inferred type is the same as the explicitly specified one, the explicit declaration is unnecessary, and the compiler issues a warning.
Constant Declarations
Constant values are preferrably declared using the "const" keyword instead of "var". In terms of naming convention, constant values are spelled in all uppercase, with different words separated with underscores:
const SAMPLE = 0
const OTHER_SAMPLE = 10
const SAMPLE_STRING = "testing"
const DOUBLE_CONSTANT = 10.5
Property Declarations
A "property" in Sling is a variable that automatically has a "setter" and "getter" method created to access the variable. Properties can only be declared for entity variables (not for local variables inside functions). Consider the following property declaration:
class MyClass
{
prop myProperty as int
}
The above code is fully equivalent to the following (indeed, the following kind of code is automatically generated by the compiler, when the compiler encounters the code written above):
class MyClass
{
var myProperty as int
func getMyProperty as int
{
return myProperty
}
func setMyProperty(value as int) as this
{
myProperty = value
return this
}
}