RFR: Generate Java enums
Marcono1234
duke at openjdk.org
Wed Jul 16 19:39:53 UTC 2025
On Mon, 7 Jul 2025 21:34:38 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>>> What, in Java, gives you this OR-like functionality for enums? EnumSet!
>>
>> Modelling each enum value as an enum set is certainly possible. It also sounds as overkill. One of the principles of jextract is to add as little overhead as possible. I think here we should just be honest and admit that C enums and Java enums don't have much in common, except for the use of the word `enum`.
>
>> > What, in Java, gives you this OR-like functionality for enums? EnumSet!
>>
>> Modelling each enum value as an enum set is certainly possible. It also sounds as overkill. One of the principles of jextract is to add as little overhead as possible. I think here we should just be honest and admit that C enums and Java enums don't have much in common, except for the use of the word `enum`.
>
> (note -- this does NOT mean that other, more specialized, code generators could not model C enums as Java enums. Perhaps in some code bases that makes total sense. But jextract is intended to provide reliable bindings for _all_ C headers -- which sort of restricts the kind of tricks we can play)
> > Hey @mcimadamore please excuse my gap in understanding here, but won't this operation be possible in Java by doing: `Color.Red.ordinal() | Color.Blue.ordinal()`?
>
> As said in another comment, there's no guarantee that the Java enum ordinal and the value of the C enum constant will be the same. So, no, that would not be a good translation. Also, please consider clients passing enums to native functions -- right now they can just say e.g.
>
> ```java
> native_func(RED);
> ```
Isn't this what the enum `getValue()` method included in this PR is for? E.g. `Color.Red.getValue() | Color.Blue.getValue()` and `native_func(RED.getValue())`.
But the other concerns mentioned above still remain, such as making it difficult to discover which enum to use when a jextract-generated method just accepts an `int`, and that users might still accidentally use `ordinal()` or `MyEnum.values()[i]` instead of the `getValue()` method.
-------------
PR Comment: https://git.openjdk.org/jextract/pull/284#issuecomment-3080028890
More information about the jextract-dev
mailing list