How to implement the abstract function in Enum constants

advertisements

What I'm trying to achieve is a simple pattern that I'm using in Java and should be do-able in Kotlin according to the documentation. I just want to declare an enum class with a couple of constant definitions that implement the same abstract functions.

My problem is that I cannot manage to make my code compile. I always get the same error:

modifier abstract not allowed here

Here is the code:

enum class Program {
    HOME {
        override fun readableName(context: Context): String {
            return context.getString(R.string.program_home)
        }
    },
    WEEKEND {
        override fun readableName(context: Context): String {
            return context.getString(R.string.program_weekend)
        }
    },
    SHOPPING {
        override fun readableName(context: Context): String {
            return context.getString(R.string.program_shopping)
        }
    };

    abstract fun readableName(context: Context): String
}

I have even tried with the sample code from the documentation and yet even this does not compile.

Could anyone have an idea about this odd issue? Btw I'm currently using Kotlin 1.0.6.


I just tried your code and it's compiled normally with Kotlin plugin 1.0.6-release-Studio2.2-1

Maybe you can just try to update AndroidStudio/Kotlin plugin ?