-
Notifications
You must be signed in to change notification settings - Fork 0
Tuples
Tuples is an ActionScript3 language extension.
“Tuples” is also a name of an ordered set of data objects containing two or more components. In our daily life tuples are commonly found everywhere, peculiarly in method calls. Code Orchestra supports the two kinds of tuples: indexed tuples and named ones. Tuples always a specific number (or name, when it comes to a named tuple) and a fixed sequence of elements.
##Using Tuples
The most general use-case of (indexed) tuples is a situation when a function has to return not a single value but a certain set of values of the estimated types.
##Indexed Tuples Syntax
Indexed tuples appear in two incarnations: tuple type and tuple expression.
###Tuple expression
[ valueA, valueB, …, valueZ ]
Indexed tuple expression stores all the values of a tuple.
/**
* method() should return a Tuple type
*/
public function method() : [String, int, Sprite] {
// tuples expression is placed in square brackets
return ["Hello", 42, this];
}
###Tuple type
[ typeA, typeB, …, typeZ ]
Indexed tuple type can describe a type of any expression.
// myVar is a variable of a Tuple type
var myVar : [String, int, Sprite] = method();
variableName[index];
As is already clear from the name, indexed tuples can be accessed by index. Note, that a typesystem check and autocomplete work in this case. Since tuples are always of a fixed size, calling the wrong index produces an error.
var myVar : [String, int, Sprite] = method();
var a1 : String = myVar[0];
var a2 : int = myVar[1];
var a3 : Sprite = myVar[2];
myVar[3]; // IDE reports an error "out of bounds"
A little bit more complicated sample of tuples accessing can be shown using the Collections language extension (you can also try to repeat it in a pure AS3 with Vector
instead of List
, but we don’t officially recommend such a substitution).
var myList : list<[String, int, Sprite]> = new list<[String, int, Sprite]>;
myList.add(["Hi there!", 11, this]); // a valid code
myList.add([12, "a string", this]); // type system will report an error
##Named Tuples Syntax
public tuple Name {
nameA : typeA;
…
nameZ : typeZ;
}
Named tuple declaration has much in common with a class declaration. Right after importing the Tuples language extension, a new named tuple declaration can be created from the package context menu in the project window (same way as a new root creation).
public tuple myNamedTuple {
username : String;
birthdate : Date;
email : String;
}
( nameA = value, nameB = value, …, nameZ = value )
Named tuple type can describe a type of any expression (same way as index typle type do). Name tuple expression stores all the values of a tuple.
/**
* method() should return a named Tuple type
*/
public function method() : (username, birthdate, email) {
// name tuples expression
return (username = "john", birthdate = new Date(1945), email = "[email protected]");
}
variableName.name
Named tuples can accessed practically same way as indexed tuples with the only difference that name is used instead of index. Name autocompletion and type system checks will work in IDE.
var user : (username, birthdate, email) = method();
var a1 : String = user.username; // on variable creation
var a2 : Date = user.birthdate; // IDE will automatically offer
var a3 : String = user.email; // the right variable type