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

Casts

An expression of the form:

data-type monadic-expression

in which a type name is followed by an expression is called a cast, and converts the monadic expression to the named type. Only certain specialized forms are provided for.

Arithmetic Casts

In arithmetic casts, the named type must be one of byte, int, big, or real, and the monadic-expression must have arithmetic type. For example:

byte 10

is an expression of byte type and value 10. When real values are converted to integral ones, they are rounded to the nearest integer. The effect of overflow during conversion is undefined.

Casts to Strings

Here the named data type is string. In a first form, the monadic expression has type int, big, byte, or real, and the value is a string containing the decimal representation of the integer, which can be positive or negative. In a second form, the monadic expression has type array of byte. The value is a new string containing the Unicode characters obtained by interpreting the bytes in the array as a UTF-8 representation of that string. (UTF-8 is a representation of 16-bit Unicode characters as one, two, or three bytes.) The result of the conversion is undefined if the byte array ends within a multi-byte UTF-8 sequence.

Casts from Strings

In a first form, the monadic expression is a string, and the named type is int, byte, big, or real. The value is obtained by converting the string to an integer. Initial white space is ignored; after a possible sign, conversion ceases at the first non-digit. (Note that a real can contain a single decimal point.) In a second form, the named type is array of byte and the monadic-expression is a string. The value is a new array of bytes containing the UTF-8 representation of the Unicode characters in the string. For example,

s := "Ångström"; 
a := array of byte s;
s = string a;

takes the string s apart into bytes in the second line, and puts it back in the third. The length of s is 8, because it contains that many characters; the length of a is larger, because some of its characters require more than one byte in the UTF-8 representation.

Casts to adt and ref adt

Here the named type is that of an adt or ref adt, and the monadic expression is a comma-separated list of expressions within parentheses. The value of the expression is an instance of an adt of the named type whose data members are initialized with the members of the list, or whose single data member is initialized with the parenthesized expression. In case the type is ref adt, the value is a reference to the new instance of the adt.

The expressions in the list, read in order, correspond with the data members of the adt read in order; their types and number must agree. Placement of any function and constant members of the adt is ignored. For example:

Point : adt {
	x : int;
	two : con 2;
	eq : fn (p: Point): int;
	y : int;
};
. . .
p : Point;
p = Point(1, 2);

puts in p a Point whose x value is 1 and whose y value is 2.



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

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