Zsolt Bencze
01/27/2023, 2:13 PMjw
01/27/2023, 2:15 PMephemient
01/27/2023, 2:17 PMenum Foo {
case bar, baz
}
is mostly equivalent to
enum class Foo {
bar, baz
}
you can't translate
enum Foo {
case bar(Int)
case baz
}
to a Kotlin enum class
Zsolt Bencze
01/27/2023, 2:21 PMephemient
01/27/2023, 2:21 PMsealed class Foo {
data class Bar(val value: Int) : Foo()
object Baz : Foo()
}
instead. of course you could also translate an enum
without associated values to `sealed class`+`object`, but while the usage is pretty similar in Kotlin, you lose some of the Java conveniences of enum
that waykevin.cianfarini
01/27/2023, 2:22 PMephemient
01/27/2023, 2:22 PMjw
01/27/2023, 2:24 PMkevin.cianfarini
01/27/2023, 2:25 PMjw
01/27/2023, 2:25 PMephemient
01/27/2023, 2:27 PMenum class Foo {
Bar,
Baz {
override fun toString(): String = "baz"
}
}
in bytecode this looks sorta like what
class Foo private constructor(override val name: String) : Enum<Foo> {
override fun toString(): String = name
private class Baz(name: String) : Foo(name) {
override fun toString(): String = "baz"
}
companion object {
@JvmField
val Bar = Foo("bar")
@JvmField
val Baz = Baz("baz")
@JvmStatic
fun values(): Array<Foo> = arrayOf(Bar, Baz)
}
}
would compile to, except that you aren't allowed to subclass Enum
yourselfZsolt Bencze
01/27/2023, 4:52 PM