🐦 Kotlin
Advanced
What is a function type with receiver in Kotlin?
Answer
A function type with receiver is written as ReceiverType.(Parameters) -> ReturnType and describes a function where this inside the function body refers to an instance of the receiver type. This is the foundation of Kotlin DSLs and scope functions. Example: val appendHello: StringBuilder.() -> Unit = { append("Hello") } — inside the lambda, this is a StringBuilder. You call it on an instance: val sb = StringBuilder(); sb.appendHello(). The scope functions apply, with, and run all use function types with receivers internally.