Logo

Programming-Idioms

  • Pascal
  • C++
  • Elixir
  • JS
  • Groovy

Idiom #220 Create a tuple value

Create t consisting of 3 values having different types.

Explain if the elements of t are strongly typed or not.

let t = [2.5, "hello", -1];

An Array may hold any list of values.
Elements are not strongly typed.
type
  Tuple = record
    a: integer;
    b: string;
    c: boolean;
  end;
var
  t: Tuple;
begin
  t := Default(Tuple);
end.

Elements of type Tuple are strongly typed.
The Default intrinsic will create a variable of type Tuple with all it's members set to their respective default values.
#include <tuple>
std::tuple<float, std::string, bool> t(2.5f, "foo", false);

Strongly typed, can include any primitive or class and be of arbitrary (but fixed) length.
#include <tuple>
auto t = std::make_tuple(2.5f, std::string("foo"), false);

Types are inferred.
let t = 1, 'a', "b"

in OCaml, parenthesis for tuples are optional and only used to disambiguate.
Type inference can determine the type of the tuple without annotation.

New implementation...
programming-idioms.org