Codementor Events

Puzzlers on Kotlin Academy, week 2

Published Apr 17, 2018
Puzzlers on Kotlin Academy, week 2

Originally published at blog.kotlin-academy.com

Time for new battery of Kotlin puzzlers. In this week we’ve concentrated more on teaching and showing surprising facts about Kotlin. Have fun 😃

Lambda runnables

fun run() {
    val run: () -> Unit = {
        println("Run run run!")
    }

    object : Runnable {
        override fun run() = run()
    }.run()
}

run()

Author: Redrield

What will it display? Some possibilities:
a) “Run run run!”
b) Doesn’t compile
c) StackOverflowError
d) None of the above

Check out answer and explanation using this link or by reading this article till the end.

Making open abstract

open class A {
    open fun a() {}
}

abstract class B: A() {
    abstract override fun a()
}

open class C: B()

a) Compiles fine
b) Error: Class ‘C’ is not abstract and does not implement abstract base class member
c) Error: ‘a’ overrides nothing
d) Error: Function ‘a’ must have a body

Author: Marcin Moskala

This fact is not well known, but it might help you one day 😉

Check out answer and explanation using this link or by reading this article till the end.

List minus list

val list = listOf(1, 2, 3)
print(list - 1)
print(list - listOf(1))

val ones = listOf(1, 1, 1)
print(ones - 1)
print(ones - listOf(1))

What does it display? Some possibilities:
a) [2, 3][2, 3][1, 1][1, 1]
b) [2, 3][2, 3][1, 1][]
c) [1, 3][2, 3][][1, 1]
d) [2, 3][2, 3][][]

Author: Marcin Moskala

Another peace of surprising knowledge that can save you one day from hours of searching for error 😉

Check out answer and explanation using this link or by reading this article till the end.

Answers and explanations

For “Lambda runnables” correct answer is:

a) “Run run run!”

Why? Here is an explanation:

In object expression, Kotlin prefers to use run defined as a local variable. Be careful! This won’t work well:

val run: () -> Unit = {
    println("Run run run!")
}

fun run() {
    object : Runnable {
        override fun run() = run()
    }.run()
}

Nor this:

fun run() {
    val run: () -> Unit = {
        println(“Run run run!”)
    }
    object : Runnable {
       override fun run() = this.run()
    }.run()
}

For “Making open abstract” correct answer is:

b) Error: Class ‘C’ is not abstract and does not implement abstract base class member

Why? Here is an explanation:

We can override an open function with abstract one, but then it is abstract and we need to override it in all subclasses. This works fine:

open class A {
    open fun a() {}
}

abstract class B: A() {
    abstract override fun a()
}

open class C: B() {
    override fun a() {}
}

C().a()

For “List minus list” correct answer is:

b) [2, 3][2, 3][1, 1][]

Why? Here is an explanation:

List<T> minus T removes the first element that equals. List<T> minus List<T> filters out all elements that equal to any element on the second list.


Do you have your own idea for a puzzler? Submit it on Kotlin Academy portal.

Do you want more puzzlers in the future? Track Kotlin Academy portal or subscribe to our mailing list.

To be up-to-date with great news on Kotlin Academy, subscribe to the newsletter, observe Twitter and follow.

If you like it, remember to clap. Note that if you hold the clap button, you can leave more claps.

Discover and read more posts from Kotlin Academy
get started
post commentsBe the first to share your opinion
Show more replies