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
Hello!
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