Ilya I. Lubenets
Ilya I. Lubenets
Explore posts from servers
MModular
Created by Ilya I. Lubenets on 3/17/2024 in #questions
Kotlin's extension function in Mojo?
Not so long ago, I've discovered that Kotlin have a possibility to extend already defined classes with a new function. For example:
fun String.getWordsList(): List<String> {
return this.split(" ")
}

fun main() {
val text: String = "Hello world!"
val words = text.getWordsList()
println(words)
}
fun String.getWordsList(): List<String> {
return this.split(" ")
}

fun main() {
val text: String = "Hello world!"
val words = text.getWordsList()
println(words)
}
will return [Hello, world!] list. This feature is really useful, as it makes code really cleaner without overriding whole class. I don't asking Modular team to implement it. I'm just curious what others think about that. Is Mojo needs that or static methods is enough? I mean, we can use text.getWordsList() is Kotlin and get_words_list(text) in Mojo and Python. What is better for you?
9 replies