skip to content

How do you create a Pair in Kotlin, and what does the 'to' infix function do?

level: juniorimportance: must knowfreq 70%

answer

  1. 'to' = infix sugar for Pair(this, that)
  2. .first / .second / .third
  3. data class → equals/hashCode/copy free
  4. mapOf("a" to 1) — 'to' shines for maps
  5. Triple has no infix builder

basics

~10 s

A Pair holds two values. You make one with Pair(a, b) or, more commonly, with the 'to' word, like 1 to "one". 'to' just builds a Pair of its left and right values.

solid answer

~30 s

Pair<A, B> is a generic data class holding two values, accessed via .first and .second. You construct it with the Pair(a, b) constructor or the infix function 'to', defined as `infix fun <A, B> A.to(that: B): Pair<A, B>`, so `"key" to 42` produces Pair("key", 42). Because 'to' is just sugar for the constructor, it is most idiomatic when building maps: `mapOf("a" to 1, "b" to 2)`, where mapOf takes vararg Pair entries. Being a data class, Pair gets equals/hashCode/toString and copy for free. Triple<A, B, C> is the three-element analogue with .first/.second/.third.

code

kotlin · 5 lines
kotlin
val p = "key" to 42          // Pair<String, Int>
println(p.first)             // key
println(p.second)            // 42
val ages = mapOf("Ann" to 30, "Bob" to 25)
val t = Triple(1, "two", 3.0)

go deeper

for a junior

Can create a Pair both ways and knows .first/.second and the mapOf use case.

for a middle

Explains 'to' as an infix extension fun and notes Pair/Triple are data classes with generated members.

for a senior

Mentions precedence pitfalls and that Triple lacks an infix builder; reasons about when tuples vs named types are appropriate.

for a principal

Frames 'to' as readability sugar with no runtime magic and advises named data classes over tuples in public APIs for maintainability.

## What Pair and Triple are `Pair<A, B>` and `Triple<A, B, C>` are tiny **data classes** in the Kotlin standard library that bundle 2 or 3 values of possibly different types. They are Kotlin's lightweight tuples. - `Pair` exposes `first: A` and `second: B`. - `Triple` exposes `first: A`, `second: B`, `third: C`. Because they are `data class`es, the compiler generates `equals`, `hashCode`, `toString`, `copy`, and the `componentN` functions used for destructuring. ## Constructing a Pair There are two ways: ```kotlin val p1 = Pair("key", 42) // explicit constructor val p2 = "key" to 42 // infix 'to' function ``` The `to` function is defined in the standard library as: ```kotlin public infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that) ``` The `infix` keyword lets you call it without dots or parentheses (`a to b` instead of `a.to(b)`). It is generic over the receiver `A` and parameter `B`, so it works for any two types. ## Why 'to' exists — building maps The primary idiomatic use is constructing maps, because `mapOf` accepts `vararg pairs: Pair<K, V>`: ```kotlin val ages = mapOf("Ann" to 30, "Bob" to 25) ``` Each `"Ann" to 30` is a `Pair<String, Int>`; `mapOf` reads `.first` as the key and `.second` as the value. ## Triple ```kotlin val t = Triple(1, "two", 3.0) // Triple<Int, String, Double> ``` There is no infix builder for `Triple` — you always use the constructor. ## Gotcha: precedence `to` has low precedence, so `1 + 1 to 2` parses as `(1 + 1) to 2` = `Pair(2, 2)`, which is usually fine, but mixing `to` with other operators can surprise you. When in doubt, parenthesize.

  • Is 'to' available for Triple?
    No. There is no infix builder for Triple; you must call the Triple(a, b, c) constructor directly.
  • What does 'infix' actually do here?
    It allows calling the single-parameter member/extension function without a dot and parentheses, so `a.to(b)` can be written `a to b`.

'to' is like a stapler that clips two slips of paper (a key and a value) into one labelled bundle.

saying these in an interview costs you the question

  • Thinking 'to' is a language keyword rather than a stdlib extension function
  • Believing Pair stores values by name/key rather than positionally as first/second
  • Claiming there is a 'to' builder for Triple
  • Confusing 'to' the Pair builder with the range operator or downTo

context