ModularM
Modular2y ago
8 replies
Ilya I. Lubenets

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)
}

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?
Was this page helpful?