pkl icon indicating copy to clipboard operation
pkl copied to clipboard

codegen-java/kotlin: Support generating sealed classes

Open odenix opened this issue 1 year ago • 5 comments

It would be very nice to generate sealed Java/Kotlin classes whenever possible. This would improve code comprehension, pattern matching, and the IDE experience.

Potential solutions:

  1. Infer which abstract and open Pkl classes can be sealed in Java/Kotlin. At a minimum, this would require passing all Pkl modules at once to Java/KotlinCodeGenerator so that it can check if a class is subclassed in another module. Currently, Java/KotlinCodeGenerator only accepts a single module whereas CliJava/KotlinCodeGenerator accepts multiple.
  2. Support sealed classes in Pkl. This feels like a cleaner solution and could prove equally useful as supporting open vs. non-open classes.

odenix avatar Nov 12 '24 17:11 odenix

Infer which abstract and open Pkl classes can be sealed in Java/Kotlin.

Without a sealed in Pkl, there's no way to ensure that the provided set of classes is exhaustive. For example:

  1. Library publishes Pkl bindings + Java codegen'd classes
  2. Application uses library, extends Pkl classes and does is own codegen

bioball avatar Nov 13 '24 00:11 bioball

I think the conclusion is that moving forward with this feature would require a sealed SPICE.

odenix avatar Nov 13 '24 01:11 odenix

Yeah; I think that's right

bioball avatar Nov 13 '24 01:11 bioball

How about reserving the keyword sealed for now?

odenix avatar Nov 15 '24 01:11 odenix

I'm okay with that!

@stackoverflow, @holzensp: thoughts?

bioball avatar Jan 24 '25 22:01 bioball