81. The concept of the “this” expression in Kotlin
In Kotlin, the keyword this refers to the current object — just like in many object-oriented languages. It gives access to:
1. this in a Class
class User(val name: String) {
fun printName() {
println(this.name) // or simply println(name)
}
}
Here, this refers to the current User instance.
2. this in Extension Functions
fun String.printUpperCase() {
println(this.uppercase()) // 'this' refers to the String instance
}
Inside an extension function, this refers to the receiver object (in this case, a String).
3. this in Lambdas (and Shadowing)
In Kotlin lambdas, this may refer to:
class Outer {
val outerName = "Outer"
inner class Inner {
val innerName = "Inner"
fun printNames() {
println(this.innerName) // refers to Inner
println(this@Outer.outerName) // refers to Outer
}
}
}
this@ClassName lets you disambiguate between nested this references.
4. this in apply, with, run (Scope Functions)
In some scope functions, this refers to the receiver:
val user = User("Alice").apply {
println(this.name) // 'this' refers to the User object
}
But in let, this is not available — you use it instead:
val nameLength = "Alice".let {
println(it.length) // 'it' is the lambda argument
}
Important Table:
Context | this refers to |
---|---|
Class method | The current class instance |
Extension function | The receiver object |
Nested class | Inner class, use this@Outer for outer |
apply, run, with | The lambda receiver (this) |
let, also | Not this, use it instead |
82. The concept of default arguments in Kotlin
In Kotlin, default arguments allow you to define default values for function parameters, so callers can omit some arguments when calling the function.
Syntax
fun greet(name: String = "Guest", message: String = "Welcome!") {
println("Hello $name, $message")
}
Usage:
greet() // Hello Guest, Welcome!
greet("Alice") // Hello Alice, Welcome!
greet("Bob", "Good to see you!") // Hello Bob, Good to see you!
83. The concept of function references in Kotlin
In Kotlin, function references allow you to refer to a function by name without actually calling it. This makes functions first-class citizens, which can be:
Syntax:
::functionName
This refers to a top-level, member, or extension function without calling it.
Example 1: Top-level Function Reference
fun greet(name: String) {
println("Hello, $name")
}
val greetingRef: (String) -> Unit = ::greet
fun main() {
greetingRef("Kotlin") // Output: Hello, Kotlin
}
Example 2: Member Function Reference
class Person(val name: String) {
fun introduce() = "Hi, I'm $name"
}
fun main() {
val person = Person("Alice")
val introRef = person::introduce
println(introRef()) // Output: Hi, I'm Alice
}
84. Function Reference vs Lambda
Lambda | Function Reference |
---|---|
{ x -> println(x) } | ::println |
{ it.length } | String::length |
{ s -> User(s) } | ::User (constructor ref) |
85. What is downTo keyword in Kotlin ?
The downTo keyword in Kotlin is used to create a descending range of values — typically numbers — and is often used in for loops.
Syntax:
start downTo end
Example
for (i in 5 downTo 1) {
println(i)
}
Output:
5
4
3
2
1
With step
for (i in 10 downTo 0 step 2) {
println(i)
}
Output:
10
8
6
4
2
0
Related Keywords
Keyword | Description |
---|---|
.. | Ascending range (e.g. 1..5) |
downTo | Descending range (e.g. 5 downTo 1) |
step | Control step size in a range |