Explain the 'to' infix function used in mapOf. What does it produce, and what are its pitfalls?
answer
- to = infix fun A.to(B): Pair<A,B>
- "a" to 1 == Pair("a", 1)
- mapOf takes vararg Pair
- duplicate keys -> last wins silently
- each 'to' allocates a Pair (hot-path cost)
basics
~10 s'to' is a small function that builds a key-value Pair, like "a" to 1. mapOf takes those pairs. It is just a Pair, so it has no special map magic.
solid answer
~50 sto is a generic infix extension: infix fun <A, B> A.to(that: B): Pair<A, B>. So "a" to 1 is identical to Pair("a", 1). mapOf(vararg pairs: Pair<K, V>) consumes these pairs. Because it is just a function, the right side is any expression; precedence is low, so a to b + 1 parses as a to (b + 1). Pitfalls: (1) building a large map with mapOf creates a throwaway Pair per entry plus an array — for hot paths buildMap or a mutable map avoids that allocation overhead; (2) to has nothing to do with maps specifically — it works anywhere a Pair is wanted, e.g. destructuring val (x, y) = a to b; (3) duplicate keys: the last pair wins silently. Knowing to is plain Pair construction clarifies why mapOf is just sugar over pairs, not a special syntax.
code
kotlin · 6 linesval a = "a" to 1 // Pair("a", 1)
val m = mapOf("x" to 1, "x" to 2) // {x=2} last wins
val (lo, hi) = 1 to 9 // destructuring
// hot path: avoid per-entry Pair
val big = mutableMapOf<Int, Int>()
for (i in 0..1000) big[i] = i * igo deeper
Knows 'a' to 1 makes a map entry and mapOf uses it.
Knows 'to' produces a Pair and works for destructuring/returns too.
Explains the exact signature, duplicate-key last-wins behavior, precedence, and the per-Pair allocation cost.
Weighs mapOf-with-to vs builder/mutable approaches by allocation and readability, and reasons about silent duplicate-key bugs in data pipelines.
## What `to` actually is `to` is not syntax — it is an **infix extension function** in the standard library: ```kotlin public infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that) ``` So `"a" to 1` is **exactly** `Pair("a", 1)`. `infix` lets you write it without dots or parentheses. ## How mapOf uses it `mapOf(vararg pairs: Pair<K, V>): Map<K, V>` takes a vararg of `Pair`. The `to` just produces those pairs: ```kotlin val m = mapOf("a" to 1, "b" to 2) val same = mapOf(Pair("a", 1), Pair("b", 2)) // identical ``` There is nothing map-specific about `to`; it is general-purpose pair construction. ## Precedence pitfall Infix functions have **lower precedence** than arithmetic but you must still watch grouping: ```kotlin val p = "x" to 1 + 2 // "x" to (1 + 2) -> Pair("x", 3) ``` Mixing `to` with `..` or `in` can surprise you; parenthesize when unsure. ## Duplicate keys `mapOf("a" to 1, "a" to 2)` does **not** error — the **last** entry wins, yielding `{a=2}`. Silent overwrite is a common bug source. ## Allocation cost Each `to` allocates a `Pair`, and `mapOf` also builds an array for the vararg before constructing the `LinkedHashMap`. For a handful of entries this is negligible. For **large or hot-path maps**, prefer building into a `mutableMapOf`/`buildMap` and putting keys directly, avoiding the per-entry `Pair` and the intermediate array. (buildMap itself is a sibling topic; the takeaway here is the cost model of `to` + `mapOf`.) ## `to` is broader than maps Because it is just `Pair`, you can use it anywhere a pair helps: ```kotlin val (a, b) = 1 to 2 // destructuring a Pair fun minMax() = lo to hi // returning two values ``` Knowing `to` is plain `Pair` construction is what unlocks reading `mapOf` as ordinary function calls, not magic syntax.
- What happens with duplicate keys in mapOf?No error; the last pair for a given key wins, silently overwriting earlier ones.
- Is 'to' special to maps?No. It is a generic infix function returning Pair<A,B>; you can use it for destructuring, returning two values, or anywhere a Pair fits.
- Why might you avoid mapOf with 'to' for a large map?Each entry allocates a Pair and mapOf builds a vararg array first; a mutableMapOf/buildMap with direct puts avoids that per-entry overhead.
'to' is a stapler that clips two values into one Pair; mapOf is just a tray that collects the stapled pairs into a map.
saying these in an interview costs you the question
- Treating 'to' as special map syntax rather than Pair construction
- Assuming duplicate keys throw an error
- Not knowing 'to' works outside maps
- Ignoring the allocation cost for large maps
- Confusing 'to' (Pair) with the '..' range operator