# Extenable enum class

**URL:** <https://discuss.kotlinlang.org/t/extenable-enum-class/2522>\
**Category:** Uncategorized\
**Created:** [April 24, 2017, 9:36pm UTC](https://discuss.kotlinlang.org/t/extenable-enum-class/2522 "2017-04-24T21:36:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luistrigueiros](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luistrigueiros/32/2161_2.png) [@luistrigueiros](https://discuss.kotlinlang.org/u/luistrigueiros)\
**Post date:** [April 24, 2017, 9:36pm UTC](https://discuss.kotlinlang.org/t/extenable-enum-class/2522/1 "2017-04-24T21:36:14Z")

</div>

Hi

What is the best way to model one extendable enum, no code been extended just data.  
The idea is to have one base enum with a set of possible discrete values and extend this enum in another place with new additional values.  
What I am looking to model is kind of of a protocol of basic operations that are expected to be possible and be able to provide additional discover-able set of operations.

Thank you, kindly  
Luis Oscar

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [April 25, 2017, 9:34am UTC](https://discuss.kotlinlang.org/t/extenable-enum-class/2522/2 "2017-04-25T09:34:22Z")

</div>

It depends on what you want and at what semantic level. At base level you use an interface and have the enum implement this interface (and any other enum can do the same). You cannot close this though and loose some of the advantages of having an enum in the first place.

It depends really what you want. There are different goals:

- Ensure uniqueness (no duplicate items)
- The ability to enumerate operations (just put them in a set)
- Fast switching on values

What you probably want is some sort of protocol manager object that is responsible for providing operation objects. It could be possible to register new objects in some way, managed by the manager class. Operation objects can be an inner class of the manager and have private constructors such that only the manager can manage them. The manager can even maintain sequence numbers for efficient `when`/`switch` statements. You will basically have to write your own almost-enum class that doesn’t come with the restrictions of enum. You will have to take care of the initialisation issues manually though.

---

<div class="post-metadata">

**Author:** ![luistrigueiros](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luistrigueiros/32/2161_2.png) [@luistrigueiros](https://discuss.kotlinlang.org/u/luistrigueiros)\
**Post date:** [April 30, 2017, 7:13pm UTC](https://discuss.kotlinlang.org/t/extenable-enum-class/2522/3 "2017-04-30T19:13:48Z")

</div>

Hi @pdvrieze:

This is what I end up making:

```
import org.apache.camel.builder.RouteBuilder
interface Operation {
    val name: String
    val description: String
}
data class DefaultOperation(override val name: String, override val description: String) : Operation
object DEFAULT_OPERATIONS {
    object Availability : Operation by DefaultOperation("Availability", "Retrieve availabilities")
    object Booking : Operation by DefaultOperation("Booking", "Make a booking")
    object BookingInfo : Operation by DefaultOperation("BookingInfo", "Retrieve booking info")
    object Cancel : Operation by DefaultOperation("Cancel", "Cancel a booking")
    object Locations : Operation by DefaultOperation("Locations", "Retrieve list of locations")
}
abstract class OperationsRouteBuilder(val operations: Map<Operation, String>) : RouteBuilder()

```

---

<div class="post-metadata">

**Author:** ![rloic](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@rloic](https://discuss.kotlinlang.org/u/rloic)\
**Post date:** [May 25, 2018, 9:22pm UTC](https://discuss.kotlinlang.org/t/extenable-enum-class/2522/4 "2018-05-25T21:22:40Z")

</div>

I saw an other way to make extensive enum by combining enum and interfaces on this article : [Extensible Enum With Interface - DZone Java](https://dzone.com/articles/java-extensible-enum-with-interface)

---

<div class="post-metadata">

**Author:** ![luistrigueiros](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luistrigueiros/32/2161_2.png) [@luistrigueiros](https://discuss.kotlinlang.org/u/luistrigueiros)\
**Post date:** [May 25, 2018, 10:04pm UTC](https://discuss.kotlinlang.org/t/extenable-enum-class/2522/5 "2018-05-25T22:04:07Z")

</div>

Thank you. for the reference

---

<div class="post-metadata">

**Author:** ![piskorski.pio](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@piskorski.pio](https://discuss.kotlinlang.org/u/piskorski.pio)\
**Post date:** [April 12, 2019, 9:22am UTC](https://discuss.kotlinlang.org/t/extenable-enum-class/2522/6 "2019-04-12T09:22:32Z")

</div>

Hey I have similiar question, so I think I may join it to this discussion.

My problems related error handling in View. My business logic deliver to View generic object “Response” where in Error I have enum RemoteError. It would be fine just for remote but I want also in return there input validation Error.

So if I would send and email. My logic validates at first if email is correct, then if not it should return Response.Error containing EMAIL\_INVALID enum. Ideally would be could extend basis EnumRemoteError in order to achieve that.

> sealed class Response{  
> data class Success(…)  
> data class Error(val errorType: EnumRemoteError)  
> }
> 
> enum EnumRemoteError{  
> ERROR\_500,  
> NETWORK …  
> }
> 
> enum EnumEmail: EnumRemoteError{  
> INVALID\_EMAIL  
> }
> 
> // Error Handling in View (Activity/Fragment)
> 
> class View{  
> val response = BusinnesLogic.getResponse()  
> if(response is Response.Error){  
> val errorType = response.errorType as EnumEmail  
> }  
> }
