# Enum class with type parameters

**URL:** <https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633>\
**Category:** Language Design\
**Created:** [April 24, 2016, 6:12pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633 "2016-04-24T18:12:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivw](https://avatars.discourse-cdn.com/v4/letter/i/9e8a1a/32.png) [@ivw](https://discuss.kotlinlang.org/u/ivw)\
**Post date:** [April 24, 2016, 6:12pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/1 "2016-04-24T18:12:44Z")

</div>

Kotlin currently does not support type parameters on enums, but it would be useful to have them.

There has been some discussion here from people missing the same feature in Java:

> <https://stackoverflow.com/questions/4290878/why-shouldnt-java-enum-literals-be-able-to-have-generic-type-parameters>

Could this be possible in Kotlin?

---

<div class="post-metadata">

**Author:** ![norswap](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/norswap/32/1737_2.png) [@norswap](https://discuss.kotlinlang.org/u/norswap)\
**Post date:** [April 25, 2016, 12:48pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/2 "2016-04-25T12:48:14Z")

</div>

Do you have a **good** use-case?

The problem I see is that if you manipulate an enum member through the enum type, you can’t access the type parameter.

So you only get access to it when you manipulate the enum member by name. But then you can “harcode” the parameter everywhere it is used.

That leaves extending a generic class with different parameters for different enum members. That is indeed Lukas’ exemple:

```
public interface DataType<T> {}

public enum SQLDataType<T> implements DataType<T> {
    TINYINT<Byte>,
    SMALLINT<Short>,
    INT<Integer>,
    BIGINT<Long>,
    CLOB<String>,
    VARCHAR<String>,
    ...
}

```

But that’s only the definition. How to use that usefully?

---

<div class="post-metadata">

**Author:** ![ivw](https://avatars.discourse-cdn.com/v4/letter/i/9e8a1a/32.png) [@ivw](https://discuss.kotlinlang.org/u/ivw)\
**Post date:** [April 25, 2016, 2:39pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/3 "2016-04-25T14:39:34Z")

</div>

I’d say SQLDataType is a very good use-case example, as it is directly from jOOQ.

An example of how that is useful: in jOOQ, a query result object has the following method:

`<T> Field<T> field(String name, DataType<T> dataType)`

So you could use `result.field("name", SQLDataType.VARCHAR)` to actually get a string,  
or `result.field("id", SQLDataType.INT)` to get an int, etc.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [April 25, 2016, 3:14pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/4 "2016-04-25T15:14:01Z")

</div>

Maybe it’s better to use sealed class hierarchy in such case?

```auto
sealed class SQLDataType<T : Any>(val datatype: KClass<T>) {
    object TINYINT : SQLDataType<Byte>(Byte::class)
    object SMALLINT : SQLDataType<Short>(Short::class)
    // etc
    object VARCHAR : SQLDataType<String>(String::class)
}

```

---

<div class="post-metadata">

**Author:** ![ivw](https://avatars.discourse-cdn.com/v4/letter/i/9e8a1a/32.png) [@ivw](https://discuss.kotlinlang.org/u/ivw)\
**Post date:** [April 26, 2016, 9:37pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/5 "2016-04-26T21:37:44Z")

</div>

That would be better than using normal class hierarchy, but I think sealed classes are not really suitable if you just need a fixed set of objects. For example, with sealed classes you can’t iterate all its objects, or get an object’s ordinal.

Enums would be perfect for these cases, but for some unknown reason Java and Kotlin do not allow type parameters on them. Could it be done, or is there a reason it’s not possible?

---

<div class="post-metadata">

**Author:** ![SkittishSloth](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@SkittishSloth](https://discuss.kotlinlang.org/u/SkittishSloth)\
**Post date:** [May 1, 2016, 1:52am UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/6 "2016-05-01T01:52:21Z")

</div>

I know it’d be tedious to implement for a lot of instances, but you _could_ add an iterative accessor, ordinal accessor, and some other stuff (i.e. valueOf or something) manually. Not a great solution of course, but it could serve as a workaround at least.

---

<div class="post-metadata">

**Author:** ![Xerus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/xerus/32/4499_2.png) [@Xerus](https://discuss.kotlinlang.org/u/Xerus)\
**Post date:** [December 10, 2017, 2:30pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/7 "2017-12-10T14:30:34Z")

</div>

I REALLY WANT THIS!!

consider this enum:

```auto
enum class Conjunction(private val pred: (Predicate<Any>, Predicate<Any>) -> Predicate<*>) : (Predicate<Any>, Predicate<Any>) -> Predicate<*> 
{
    AND({ p1, p2 -> p1.and(p2) }),
    OR({ p1, p2 -> p1.or(p2) });
    override fun invoke(p1: Predicate<Any>, p2: Predicate<Any>) = pred(p1, p2)
}

```

Absence of type parameters makes it basically unusable. Could I use type Parameters, then I would replace Any with T, but I can’t, so I have to resort to a normal class now…

---

<div class="post-metadata">

**Author:** ![Xerus](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/xerus/32/4499_2.png) [@Xerus](https://discuss.kotlinlang.org/u/Xerus)\
**Post date:** [January 12, 2020, 6:33pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/8 "2020-01-12T18:33:19Z")

</div>

No news on this? It still bugs me regularly…

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [January 12, 2020, 6:56pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/9 "2020-01-12T18:56:10Z")

</div>

This subject seems related to [JEP 301: Enhanced Enums](https://openjdk.java.net/jeps/301) proposal.

---

<div class="post-metadata">

**Author:** ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)\
**Post date:** [January 12, 2020, 7:04pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/10 "2020-01-12T19:04:08Z")

</div>

> [@ivw](#):
>
> That would be better than using normal class hierarchy, but I think sealed classes are not really suitable if you just need a fixed set of objects. For example, with sealed classes you can’t iterate all its objects, or get an object’s ordinal.

Right, you don’t have ordinals like in enums, but it is possible to iterate sealed objects:

```auto
private sealed class SealedValue {
	object A : SealedValue()
	object B : SealedValue()
	object C : SealedValue()
}

fun main() {
	val sealedValues = SealedValue::class.sealedSubclasses.mapNotNull { it.objectInstance }
	sealedValues.forEach(::println)
}

```

---

<div class="post-metadata">

**Author:** ![Whathecode](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/whathecode/32/7494_2.png) [@Whathecode](https://discuss.kotlinlang.org/u/Whathecode)\
**Post date:** [May 3, 2020, 4:05pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/11 "2020-05-03T16:05:49Z")

</div>

To have a logically related iterable set of objects (without having to use reflection) I just wrote the following helper base class:

```
open class EnumObjectList<T> private constructor( private val list: MutableList<T> ) :
    List<T> by list
{
    constructor() : this( mutableListOf() )

    protected fun <TAdd : T> add( item: TAdd ): TAdd = item.also { list.add( it ) }
}

```

Which for example can be used as follows:

```
object SamplingSchemes : EnumObjectList<DataTypeSamplingScheme<*>>()
{
    val GEOLOCATION = add( Geolocation( TimeSpan.fromMinutes( 1.0 ) ) )
    val STEPCOUNT = add( Stepcount( TimeSpan.fromMinutes( 1.0 ) ) )
}

```

The members retain their full type information, and there is no need for an ‘intermediate’ enum type.

---

<div class="post-metadata">

**Author:** ![Whathecode](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/whathecode/32/7494_2.png) [@Whathecode](https://discuss.kotlinlang.org/u/Whathecode)\
**Post date:** [May 6, 2020, 12:31pm UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/12 "2020-05-06T12:31:25Z")

</div>

I wrote the full reasoning behind this up in a blog post: [List of Strongly-Typed Objects Acting Like Enum in Kotlin – whatheco.de](https://whatheco.de/2020/05/05/list-of-strongly-typed-objects-acting-like-enum-in-kotlin/)

---

<div class="post-metadata">

**Author:** ![felix](https://avatars.discourse-cdn.com/v4/letter/f/aeb1de/32.png) [@felix](https://discuss.kotlinlang.org/u/felix)\
**Post date:** [October 30, 2020, 11:07am UTC](https://discuss.kotlinlang.org/t/enum-class-with-type-parameters/1633/13 "2020-10-30T11:07:51Z")

</div>

My personal opinion very much instersects with what @Whathecode says. Antlr (Parser Generator) for example makes heavy use of that to prevent reflecting over types. So, parsers would be a good use case I guess.

Also I think that would be a very usable feature for when statements and pattern matching.  
So primary purpose is probably reducing reflecting code. Which is great for native, too, no?
