Data types
a = null;
b = true;
c = 123;
d = 123.456;
e = "text";
- There's a total of 9 data types in SGScript: null, boolean, integer, real number, string, function, C function, object and pointer.
null
is the 'no value' type. There is only one value for the type.
- Boolean is a
true
/false
type.
- Integers and real numbers are two ways to represent numbers in the language. Both have their uses, however generally they're interchangable.
- The
string
type stores an array of bytes, generally meant to store text but can be used for any data.
f = function(){ return 4; };
g = println;
h = io_file(); // returns a file object
i = toptr(100);
- Functions and C functions are not as much data types as they are code types. One represents a function defined in SGScript code and another represents a function defined in C/C++ code.
- As with numbers, both function types are generally interchangable but have some major differences.
- Objects are universal, yet somewhat heavy types with a variable number of subtypes. They can represent data structures as well as interfaces and even act like functions.
- Pointers are basically integers that have restricted manipulation capabilities and a separate processing chain, useful for passing handles and memory addresses in a safe way.
arr = [ 1, 3, 5, 7 ];
println( arr[2] ); // prints 5
println( arr[4] ); // warning: index out of bounds
println( arr.size ); // prints 4
- Array is a complex data type, a subtype of object. It contains a list of other variables.
- Sub-variables can be accessed with index
[ .. ]
and property .
operators.
- Be aware that not all sub-variables can be read and not all of them can be written. Systems will often report the same warnings for variables that don't exist and those that don't support the required operation.
- The array's
size
property returns the length of the array, the number of variables in it.
arr = [];
arr.push( 5 );
x = arr.pop();
arr.unshift( 4 );
y = arr.shift();
- Objects can have methods. Methods can be called through property access, this compiles to a special call where the object is passed through a special channel.
- Other sub-variable accessors don't support the method call, however it is possible to invoke in other ways, to be described in further sections.
- Array methods shown in the example are the stack/queue interface of the array. Push/pop deals with the end of the array, shift/unshift works on the beginning.
- More info on array and its methods can be found in the documentation: array [object]
dct = { x = 5 };
dct[ "key" ] = "value";
println( dct ); // {x=5,key=value}
fnmap = map();
fnmap[ print ] = "print";
- Objects can have non-numeric indices. All variable types are allowed for keys but not all maintain their value in all objects.
- Objects of
dict
type (generated by the dict literal {}
or function dict()
) store string keys. Documentation: dict [object]
- Objects of
map
type (generated by the map
function) store literally all kinds of keys. Documentation: map [object]
dict
object is expected to be the building block of most data stored because it is most accessible.
myObj = { x = 5, y = 7 };
function myObj.moveLeft(){ --this.x; }
myObj.moveLeft(); // method call
function myObj_moveRight(){ ++this.x; }
myObj!myObj_moveRight(); // compatible call
- There are many ways to make and two ways to call functions that work on objects.
- Making:
- 1. Create a method by specifying
<variable-name> . <property-name>
for the name.
- 2. Create a plain function.
- 3. Create an anonymous method by using the following syntax:
<variable-name> . <property-name> = function ( ) <code>
or assigning any callable to a property.
- Either way,
this
can be used inside the function to access the associated object.
- Calling:
- Method call:
<data-source> . <property-name> ( <argument-list> )
.
- Compatible call:
<data-source> . <callable-source> ( <argument-list> )
.