Kotlin Trivia 1

Here, at Localazy, Kotlin is used extensively for the development of our Android tools, and the whole backend behind Localazy is also written in Kotlin.

From time to time, we play with it just for fun and come with trivia. Let’s play with us! Let’s play with us!

The trivia

Can the code below be valid in Kotlin and print “Of course, sir!” using string concatenation?

fun main() {
    translate Android app with Localazy {
        free of charge
    }
}

The solution

Yes! It’s a completely valid Kotlin code that uses infix operators. The whole code is:

package com.localazy.trivia
private infix fun Unit.Android(body: Unit) = Unit
private infix fun Unit.with(body: Unit) = Unit
private infix fun String.of(input: String) = this.plus(input)
private fun Localazy(body: () -> String) = println(body())
private val translate = Unit
private val app = Unit
private const val free = "Of course, "
private const val charge = "sir!"
fun main() {
    translate Android app with Localazy {
        free of charge
    }
}

See the whole project on Github.

Sometimes, this can be very handy to make your code clear.

Do you like it? Do you have your own trivia? Let us know!


This is a companion discussion topic for the original entry at https://localazy.com/blog/kotlin-trivia-1