Child pages
  • Tuples

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Tuples

Tuples give you a way to group related data of different types into small collection-like data structures. In MPS, tuples are available within the jetbrains.mps.baseLanguage.tuples language.

Indexed tuples

Indexed tuple is a structure, which can contain several elements of arbitrary types and elements of which can be accessed by an index. The MPS implementation represents a tuple instance by a Java object. The usual meaning of '=' and '==' operations on Java objects within MPS remains unchanged.

Code Block
[int, string] pair = [1, "a"];
int first = pair[0];
string second = pair[1];

Named tuples

Named tuples are similar to indexed tuples, with the difference that elements are accessed by name instead of by index. To use named tuples in the model you first need to explicitly define them in your model (new ->jetbrains.mps.baseLanguage.tuples/tuple).

Code Block
(first, second)<string, int> pair = (first = "abcd", second = 1);

Declaration of Pair:

Code Block
public tuple Pair<S, T> {
  S first;
  T second;
}

Named tuple declaration

A root node of concept NamedTupleDeclaration contains a single declaration.

Code Block
[public|protected|private] tuple tupleName<TypeVar, ...> {
  elementType elementName;
}

declaration part

allowed contents

tupleName

any valid identifier

elementType

either a primitive type or any type that reduces to Java classifier

elementName

any valid identifier

Previous Next