Idea: Syntax sugar for sealed classes


#1

Currently, the syntax for creating a sealed class for an ADT is this:

sealed class Shape {
  class Circle(val radius: Double) : Shape()
  class Rect(val width: Double, val height: Double) : Shape()
}

To me, this feels repetitive and doesn’t encourage making new types, whereas these types should probably be the foundation for building apps in a functional style.

Would you consider adding some syntactic sugar to make this look cleaner/simpler? Perhaps it would be possible to reuse the “enum class” syntax:

enum class Shape {
  Circle(val radius: Double),
  Rect(val width: Double, val height: Double),
}

Thanks for considering. I’m new to Kotlin but it looks really exciting.


#2

Long ago we started with the idea of turning enums into fully-fledged GADT's. It fails when interop kicks in: the Java notion of enums is limited, and to be able to use Kotlin enums from Java, we have to stick to those limitations


#3

Is it important that "enum class" creates a Java enum? In my example I meant that it could create the same class hierarchy that "sealed class" currently does. I am not attached to the syntax "enum class", but whatever the keywords are, it would be nice if creating a new GADT had a simple syntax.


#4

Yes, it is important that enums work equally well in Kotlin and Java