angryziber / kotlin-puzzlers

A collection of Kotlin Puzzlers
419 stars 58 forks source link

New puzzler #53

Closed rpuxa closed 4 years ago

rpuxa commented 5 years ago

Hello!

class Error(val code: Int) {
     override fun toString(): String = "Error code is $code"
 }

enum class Errors(code: Int, val error: Error = error(code)) {
    ERROR_1(1),
    ERROR_2(2),
    ERROR_3(3);

    fun error(code: Int) = Error(code)
}

fun main(args: Array<String>) {
    Errors.values().forEach { println(it.error) }
}

This code throws ExceptionInInitializerError. I spent about 3 hours to find why. As it turned out there is Intelij Idea bug: if you ask Idea: "Where is error function declared", it will say: "In enum class Errors", but it's not true. It declared in kotlin standart lib. public inline fun error(message: Any): Nothing = throw IllegalStateException(message.toString()) To fix this, you need to move error function out of enum

TWiStErRob commented 5 years ago

nice one! you can't even work around with a Companion object. Did you report it on https://youtrack.jetbrains.com/issues/KT? The navigation problem that is.

rpuxa commented 5 years ago

No, I didn't report it

rpuxa commented 5 years ago

https://youtrack.jetbrains.com/issue/KT-30839