Skip to content

Document how enum values are represented as plain integers in some languages #1289

Description

@generalmimon

In #717, there was a discussion about how enum values are represented as plain integers in generated JavaScript code and how this can be surprising. @GreyCat correctly pointed out in #717 (comment) that this applies to some other languages than JS as well and that it should be documented:

This probably needs to be documented, ideally somewhere around https://doc.kaitai.io/lang_javascript.html, so it won't be a surprise that for JS (as Perl, PHP and some other languages), all enums are represented just as integers, and enum dictionaries are used solely as constants.

In Go, it's kind of weird because in our implementation, "enum types" behave a lot like type aliases of the underlying integer type and the defined enum members are really just global integer constants, but you can implement arbitrary methods on such "enum type" (which is what kaitai-io/kaitai_struct_compiler#316 does, for example).

In some languages, there may be better ways to implement enums. For example, PHP 8.1 has built-in enum support, see #1123. But in other languages, it makes sense to explain in the documentation how to work with the "plain integer" enum values.

Metadata

Metadata

Assignees

No one assigned

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions