Sobes.tech
Junior — Middle

Do you have experience using extension functions in Kotlin to extend class capabilities?

sobes.tech AI

Answer from AI

Yes, extension functions in Kotlin allow adding new functions to existing classes without the need for inheritance or modifying the original class code. This is convenient for extending the functionality of libraries or standard classes.

For example, you can add a function to count the number of vowels in a string:

fun String.countVowels(): Int {
    return count { it.lowercaseChar() in "aeiou" }
}

val text = "Hello, Kotlin!"
println(text.countVowels()) // Outputs 4

Thus, extension functions make the code more expressive and convenient for reuse.

Do you have experience using extension functions in… - sobes.tech