In Kotlin, how can I work around the inherited declarations clash when an enum class implements an interface?

后端 未结 2 1081
不思量自难忘°
不思量自难忘° 2021-01-04 11:53

I define an enum class that implements Neo4j\'s RelationshipType:

enum class MyRelationshipType : RelationshipType {
    // ...
}
相关标签:
2条回答
  • 2021-01-04 12:05

    The example above is working with an interface having a property name instead of a function name().

    interface Name {
        val name: String;
    }
    
    enum class Color : Name {
        Blue
    }
    
    0 讨论(0)
  • 2021-01-04 12:08

    it is a kotlin bug-KT-14115 even if you makes the enum class implements the interface which contains a name() function is denied.

    interface Name {
        fun name(): String;
    }
    
    
    enum class Color : Name;
           //   ^--- the same error reported
    

    BUT you can simulate a enum class by using a sealed class, for example:

    interface Name {
        fun name(): String;
    }
    
    
    sealed class Color(val ordinal: Int) : Name {
        fun ordinal()=ordinal;
        override fun name(): String {
            return this.javaClass.simpleName;
        }
        //todo: simulate other methods ...
    };
    
    object RED : Color(0);
    object GREEN : Color(1);
    object BLUE : Color(2);
    
    0 讨论(0)
提交回复
热议问题