[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Reference Types

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.

A nil Value

Reference types can be assigned to the special value represented by the keyword 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.

Arrays

Arrays are made up of ordered, indexed, homogenous data. The index begins at 0.

Figure 2-1 Array Elements
A basic array declaration looks like this:

	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 byte

This 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

Array Slices

A slice is a consecutive range of elements within an array or string (in Limbo, strings are arrays of Unicode characters). The range is specified as an index within brackets. The range specifies a start value and an end value separated by a colon. It is inclusive of the start value and exclusive of the end value.

For example:

	s := "Inferno";
	
Figure 2-2 Slice Elements
A slice of this string, s, from the 2nd to the 6th element:

	s2 := s[2:6];

The value of s2 is the string fern.

Lists

Lists are made up of ordered, non-indexed, homogenous elements. The first element in the list is the head, the remaining elements make up the tail.

Figure 2-3 List Elements
For example:

	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.
Table 2-5 Operators for list types
Operator Name Meaning
:: constructor Constructs a list
tl tail Extracts all elements after the first
hd head Extracts the first element of the list

List constructor (::)

To construct a list, use ::, 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.

List head (hd)

The list head is a single element and has the same type as the elements of the list. To extract the head of the list, use the 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

List tail (tl)

The list tail is itself a list of the same type as the original list. Using the 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;

Tuples

A tuple is a parenthesized, sequential list of data types handled as a single object.

For example:

	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.



[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Copyright © 1998, Lucent Technologies, Inc. All rights reserved.