Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

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.

class Tuple {
    String s;
    int i;
    boolean b;
}
record Tuple<A, B, C>(A a, B b, C c) {}
Tuple<String, Integer, Boolean> t
    = new Tuple<>("abc", 123, true);
record Tuple(Object ... a) {}
Tuple t = new Tuple("abc", 123, true);
record Tuple(int a, String b, boolean c) {}
var t = new Tuple(1, "hello", true);

Strongly typed.
record is available since Java 16
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