How do constructor references work in Kotlin, and what is their type? Give an example of using one as a factory.
answer
- ::ClassName references the constructor
- Type = (ctorParams...) -> Class
- Great as a factory in map/build
- Arity matches the constructor
- Generics inferred from expected type
basics
~10 sWriting ::ClassName gives you a reference to the constructor — a function value that creates instances. You can pass it anywhere a factory function is expected, and calling it builds a new object.
solid answer
~30 s`::ClassName` produces a **constructor reference**: a callable whose parameters match the constructor's and whose return type is the class. For `class User(name: String)`, `::User` behaves as `(String) -> User` and is a `KFunction1<String, User>`. It's commonly used as a factory: `listOf("Ada").map(::User)` constructs a `User` per element. References to constructors of nested or generic classes work the same way; for generics the type is usually inferred from context. This is the same `::name` syntax as function references, just resolving to a constructor instead of a `fun`.
code
kotlin · 7 linesdata class User(val name: String)
fun main() {
val make: (String) -> User = ::User
println(make("Ada")) // User(name=Ada)
println(listOf("Ada", "Linus").map(::User)) // two Users
}go deeper
Knows ::User builds Users and can use it in map.
States the type (params) -> Class and uses it as a factory; understands arity matches the constructor.
Explains overload resolution among constructors and generic type inference for the reference.
Considers constructor references in DI/factory designs and the reflection metadata exposed by the resulting KFunction.
## Constructor references Kotlin lets you reference a **constructor** with the same `::` operator, using the class name: ```kotlin class User(val name: String) val factory = ::User // (String) -> User, a KFunction1<String, User> val u = factory("Ada") // User(name=Ada) ``` The reference's **parameter list mirrors the constructor's** and its **return type is the class**. ## Using it as a factory Because it behaves like an ordinary function value, it slots into higher-order functions: ```kotlin val users = listOf("Ada", "Linus").map(::User) // [User(name=Ada), User(name=Linus)] ``` This is cleaner than `map { User(it) }` and reads as 'construct a User from each element'. ## Multiple parameters The arity follows the constructor: ```kotlin class Point(val x: Int, val y: Int) val mk: (Int, Int) -> Point = ::Point // KFunction2<Int, Int, Point> mk(1, 2) ``` ## Generic classes For a generic type, the type argument is inferred from the expected type: ```kotlin class Box<T>(val value: T) val boxInt: (Int) -> Box<Int> = ::Box // T inferred as Int ``` ## Secondary / multiple constructors If a class has overloaded constructors, the expected functional type drives which one the reference resolves to — exactly like overload resolution for function references. ## Relation to function references A constructor reference is the same mechanism as `::someFun`; the compiler just resolves `::User` to the constructor. The result is still a `KFunction`, so it carries reflective metadata about the constructor.
- How does Kotlin pick which constructor `::User` refers to if there are several?By overload resolution against the expected functional type — the same way overloaded function references resolve. The required parameter/return types select the matching constructor.
- Can you reference a constructor of a generic class?Yes; the type argument is inferred from context, e.g. assigning `::Box` to `(Int) -> Box<Int>` infers `T = Int`.
A constructor reference is a stamped order form for a factory — hand it inputs and it mints a new object each time.
saying these in an interview costs you the question
- Saying you must write `::User()` with parentheses
- Thinking constructor references return the class object/`Class<User>` rather than an instance factory
- Not knowing the arity follows the constructor's parameters
- Claiming generic constructor references need explicit reflection
- Confusing `::User` with a companion/static reference