Skip to content

Tuples

Tuples#

Tuples are lightweight collections that let you group a few related values together without creating a full class. They're perfect for returning multiple values from a function or temporarily bundling data.

What You Need to Know#

  • Tuples can hold different types of data in one structure (e.g., a string and a number together).
  • Tuples are immutable—once created, you can't change their values.
  • Tuples are ordered—each element has a fixed position you can access.
  • Tuples are best for 2-3 values. For larger structures, use a class with named fields for clarity.

When to Use Tuples#

Good for:

  • Returning multiple values from a function
  • Coordinate pairs like (x, y) or (latitude, longitude)
  • Temporary grouping of related values

Avoid for:

  • Large structures with many fields (use a class instead)
  • Long-lived data that needs clear field names
  • Data that needs methods or complex behavior

Using Tuples#

// Java uses records (Java 14+) instead of tuples
record GuitarInfo(String model, int strings, String series) {}

GuitarInfo guitar = new GuitarInfo("RG350DXZ", 6, "RG Series");
String model = guitar.model();           // Access field
int strings = guitar.strings();          // Access field
// Kotlin provides Pair and Triple
val guitar = Triple("RG350DXZ", 6, "RG Series")

// Access by position
val model = guitar.first
val strings = guitar.second
val series = guitar.third

// Destructuring
val (m, s, sr) = guitar
type GuitarInfo = [string, number, string];
const guitar: GuitarInfo = ["RG350DXZ", 6, "RG Series"];

// Access by index
const model = guitar[0];
const strings = guitar[1];

// Destructuring
const [m, s, sr] = guitar;
// Dart 3.0+ has native records
(String, int, String) guitar = ("RG350DXZ", 6, "RG Series");

// Access by position
String model = guitar.$1;
int strings = guitar.$2;

// Destructuring
var (m, s, sr) = guitar;
let guitar = ("RG350DXZ", 6, "RG Series")

// Access by index
let model = guitar.0
let strings = guitar.1

// Destructuring
let (m, s, sr) = guitar
# Create tuple
guitar = ("RG350DXZ", 6, "RG Series")

# Access by index
model = guitar[0]
strings = guitar[1]

# Destructuring
m, s, sr = guitar

Why Tuples Are Useful#

Tuples make it easy to return multiple values from a function without creating a custom class. This is especially helpful for simple cases where you just need to bundle a few values together temporarily.

For example, suppose you want a function that returns both a person's name and age. Without tuples, you'd need to create a class just for this simple task. With tuples, you can return both values directly:

// Java uses records (Java 14+)
record PersonInfo(String name, int age) {}

public static PersonInfo getNameAndAge() {
    return new PersonInfo("Alice", 25);
}

// Usage
PersonInfo person = getNameAndAge();
String name = person.name();
int age = person.age();
System.out.println(name + " is " + age); // Alice is 25
fun getNameAndAge(): Pair<String, Int> {
    return Pair("Alice", 25)
}

// Destructuring makes it easy
val (name, age) = getNameAndAge()
println("$name is $age") // Alice is 25
function getNameAndAge(): [string, number] {
    return ["Alice", 25];
}

const [name, age] = getNameAndAge();
console.log(`${name} is ${age}`); // Alice is 25
// Dart 3.0+ records
(String, int) getNameAndAge() {
    return ("Alice", 25);
}

var (name, age) = getNameAndAge();
print('$name is $age'); // Alice is 25
func getNameAndAge() -> (String, Int) {
    return ("Alice", 25)
}

let (name, age) = getNameAndAge()
print("\(name) is \(age)") // Alice is 25
def get_name_and_age() -> tuple[str, int]:
    return ("Alice", 25)

name, age = get_name_and_age()
print(f"{name} is {age}")  # Alice is 25

Tuples are a simple, lightweight solution for returning multiple values when you don't need the overhead of a full class structure.