A composite type is created using a group of elements all having same type or  mixed types. We use composite types to create collections of elements. They can have names or can be accessed using an index. The index can be an native type: Number or  a String depending on the collection type.

Implementation

Level dialect implement composite types gradually.

  • Level-1: {Tuple, Record, Vector, Matrix, Option };
  • Level-2: {List, Set, Map, Unicode, Variant};
  • Level-3: {Table, View};

Tuple

A tuple is a finite sequence of elements. In mathematics, an n-tuple is an ordered list of n elements, where n is a non-negative integer. Level language use this concept very frequent to define a composite collection of something. Tuples can be declared with a name or anonymous used in expressions.

Tuple literal

The tuple literal is an enumeration of elements enclosed in round brackets and separated by comma like this: (1,2,’a’,’b’, ‘c’). Tuples are immutable and have fixed length. A tuple can be assign to a variable or can be anonymous. Tuples can be initialized with literals or can be result of a function.

Declaring a tuple variable

<tuple_name>: Tuple of (type,[,type]...);

Using type inference

<tuple_name>=(type,[,type]...);

Empty tuple

An empty tuple is called “unit” and is represented like this “()”. A program call a procedure or function using a tuple to enumerate parameter values. For functions that do not have parameters we use unit () after the name of the function to differentiate between a function reference and a function call.

  -- calling a function with no parameters
  <variable> = <function_name>();

Examples:

let
  -- example of tuple declaration 
  v_example:Tuple of (String, Integer, Integer, String);  
do
  -- example of tuple initialization
  v_example:=('a', 100, 200, "this is the tuple");

  -- following logical expressions are true
  print(v_example[0]); --> 'a'
  print(v_example[1]); --> 100;
  print(v_example[2]); --> 200;
  print(v_example[3]); --> "this is the tuple";
done;

Usage

Tuples are very important language components:

  • We use tuples to represent a list of parameters;
  • We use tuples to create a list of arguments;
  • When a method do not have parameters we use the unit tuple “()”
  • A function can return a tuple as result.
  • We can capture results of a function into multiple results separated by comma.
  • Concatenation operator can use a tuple for the right side of the operation.
  • A tuple can represent an enumeration of expressions in λ-functions.
  • A tuple is internally used to extract (key, value) pairs using “in” operator into a for loop.

Note: A tuple collection is similar to a record except that elements do not have names. Instead the elements of a tuple can be identified by subscript [x]  where x in in range (0 .. n-1).

 

You need to login to view the rest of the content. Please . Not a Member? Join Us