Language Semantics
Generally in SSL all primitive types are values and all object types are pointers. The only exception to this involves using the ref keyword to qualify the type of a function parameter.
Details
Details are as follows:
- Primitive type declarations use value semantics.
- Pointer and reference semantics are not supported for value types, except as function parameters.
- Object allocation expressions use pointer semantics, but do not require you to use/type the * and -> operators.
- You omit the * operator and use the . operator instead of the -> operator.
- Primitive types passed to functions use value semantics unless you use the ref keyword.
- Objects passed to functions use pointer semantics, but the pointers themselves are passed by value unless you use the ref keyword.
- Move semantics are provided for certain objects, on a function-by-function basis.
- All array-derived classes, <Image>, and <VariableNode>-derived objects support move semantics.
Examples
Several examples are provided below.
-
The following example declares an integer variable named x
and initializes it to the literal expression 10. In this example, x is purely a value
and that is all it can ever be.
int x = 10;
-
The following function takes an integer by value, increments it, and returns the new value.
However, the actual value of increment doesn't change at the call site. The value is
copied into the function, and the value of the copy is changed. The original value of the
variable ( prior to the function call ) remains unchanged after the function call executes.
function int Foo( int increment ) { ++increment; return increment; }
-
The following function takes an integer by reference and increments it.
Note that the keyword ref means the parameter declaration is equivalent to int & increment in C++. In this
example, the variable is transferred to the function and modified. The original value of
the variable is changed after the function call executes.
function void Foo( ref int increment ) { ++increment; return increment; }
-
The following example declares an object variable named myObject. The object is allocated on the heap, but
the use of the keyword auto means that we don't have to use delete.
auto MyClass myObject; //This statement is equivalent to using a smart pointer variation in C++ 11 and higher. (std::unique_ptr<MyObject> myObject;)
-
The following function takes a pointer to an object of type MyObject. The object is modified inside the
function, and this modification persists after the function call.
function void Foo( MyObject object ) { object.Name = "Name"; }
-
This statement is equivalent to the following C++.
void Foo( MyObject * object ) { object->SetName( "Name" ); }
-
Note that, unlike in C++, in SSL the pointer itself is passed in by value. So the following function will not change object to temp.
function void Foo( MyObject object ) { MyObject temp = new MyObject; object = temp; }
-
The following function WILL change object to temp because the pointer is passed in by reference.
function void Foo( ref MyObject object ) { MyObject temp = new MyObject; object = temp; }
To summarize, in SSL, all objects are pointers, but you do not have to use
the * operator, and you use the . operator instead of the -> operator to access object members.