Limbo reference types are similar to pointers in other languages, such as C/C++.
The primary difference between Limbo reference types and pointers is that Limbo does not allow mathematical manipulation of references. This provides for "pointer-safety" within Limbo, since a reference can never point to an illegal address. It can have a nil
value, however.
nil
. This removes the reference and its associated storage. If it is the last reference to the data, its resources are automatically and immediately reclaimed by the garbage collector.
At declaration, reference types do not have any associated storage; their value is nil
. A value must be assigned to it before the reference type can be used.
buf : array of byte;This declares the variable
buf
to be of type array of byte
.
The constructor to allocate the space for the array looks like this:
array[80] of byteThis expression creates a new byte array with 80 elements. To assign the storage of this array to a variable, use the assignment operator:
buf = array[80] of byte;Arrays can be declared and, if at the top level, initialized to 0 (zero), in one statement using the combined
:=
(colon equals) operator.
buf := array[80] of byte;The following prints a number and its square:
count := array[5] of int; i := 0; do { count[i] = i; sys->print("%d\t%d\n", i, count[i]*i++); } while (i < 5);The output looks like this:
0 0 1 1 2 4 3 9 4 16
s := "Inferno";![]()
s
, from the 2nd to the 6th element:
s2 := s[2:6];The value of
s2
is the string fern
.
line : list of string;This declares the identifier
line
to be of type list of string
. The elements in line
are string
types.
List elements are accessed, modified, and manipulated using list operators. Table 2-5 defines the list operators.
Operator | Name | Meaning |
---|---|---|
::
| constructor | Constructs a list |
tl
| tail | Extracts all elements after the first |
hd
| head | Extracts the first element of the list |
::
, the list constructor operator, such as:
line = "First" :: line;This is similar to pushing an element on to a stack. The list constructor is right associative. The right operand is a single element and must have the same type as the elements of the list. The list identifier is the left operand.
hd
operator. This does not create a new list or change the original list.
For example, using the line
list created above:
sys->print("The list head is: %s", hd line);This prints:
The list head is: First
tl
operator and assigning its value back to the original list removes the first element from the list, such as:
line = tl line;A new list can be created by assigning the tail of one list to another, such as:
args := tl line;
retcode := (0, "Success");This creates a tuple,
retcode
, that stores an integer and a string.
A tuple is unpacked through assignment. For example:
err : int; msg : string; (err, msg) = retcode;This assigns
0
to err
and the string "Success
" to msg
.
Using declare-and-unpack shorthand, the same unpacking could be done in one statement:
(err, msg) := retcode;This declares
err
and msg
and assigns them the values from retcode
.
Use the nil
keyword to ignore part of a tuple. For example:
(err, nil) = retcode;This assigns the integer value from
retcode
, but ignores the string value.