pkl
pkl copied to clipboard
Missing union type support for Kotlin codegen
Execution failed for task ':pkl-playground:genKotlin'.
> Pkl union types are not supported by the Kotlin code generator.
Bit of a tall ask, I know, but it'd be wonderful if we could map union types into some form of Kotlin structure.
It'd be great to support this, but it's not clear how they should turn into Kotlin. Feel free to provide suggestions, though.
For now this will have to be implemented with sealed class https://kotlinlang.org/docs/sealed-classes.html
yup, most likely. sealed class needs a name though, maybe take a similar approach to string union to enum mapping that all union types should be defined via a type alias?
E.g. this wouldn't still map to Kotlin:
class Foo {
items: Listing<Bar | Baz>
}
but this would?
typealias FooItem = Bar | Baz
class Foo {
items: Listing<FooItem>
}
Hi all, I'm evaluating pkl as a configuration language for an API GW. My server is in Kotlin and not having union mapped to Kotlin (or Java) is a show stopper.
There any thoughts about adding them? or what would be a workaround for something with the same semantic but supported by Kotlin?
My scenario is something like typealias HttpMethod = "GET" | "POST" | "PUT" | "DELETE"
Thanks
@giordano-newday: We don't have a great way to model most union types as Kotlin, because it's missing anonymous sum types. However, typealiases of string literal unions are a special case, and are no problem to represent.
This pkl:
typealias HttpMethod = "GET" | "POST" | "PUT" | "DELETE"
Turns into the following Kotlin:
enum class HttpMethod(
val value: String
) {
GET("GET"),
POST("POST"),
PUT("PUT"),
DELETE("DELETE");
override fun toString() = value
}
I think it would for my scenarios. Thanks @bioball.