Named integer literals are not allowed in enum type definition
See original GitHub issueRepro steps
I am writing custom bindings for the GLFW library in F#. In doing so, I want to define modules that map as closely as possible to the exported DLL functions, macros, and typedefs. As such, I want to define named integer literals for the various errors that can be returned as:
/// No error has occurred.
[<Literal>]
let GLFW_NO_ERROR = 0
/// GLFW has not been initialized.
[<Literal>]
let GLFW_NOT_INITIALIZED = 0x00010001
That works fine. I then want to define an idiomatic F# module that wraps all of the bindings and creates a more native F# experience but still closely tied to the bindings. So I would like to define an error enum for pattern matching purposes, such as:
type GLFWError =
| NoError = GLFW_NO_ERROR
| NotInitialized = GLFW_NOT_INITIALIZED
However, this is not allowed for some reason. The error is FS0010: Unexpected identifier in union case
. According to the documentation for literal attributes, the named literals are compiled to constants. It isn’t clear if the above enum error is expected or not, but given that the named literals are compiled to integer constants, I don’t currently see a reason why this limitation should be in place.
Expected behavior
I am not entirely sure what the F# compiler’s expectation is, but from a user’s perspective, I expected this to just work. In that, I expected something like the above to be identical to the case where if I defined the enum with the direct integer literals.
Actual behavior
Error FS0010: Unexpected identifier in union case
is given.
Known workarounds
Define the enum with integer literals and not named integer literals. However, I find this to be unsatisfactory from a design perspective.
Related information
Provide any related information (optional):
- Operating system: Windows 11
- .NET Runtime kind (.NET Core, .NET Framework, Mono): .NET 6
- Editing Tools (e.g. Visual Studio Version, Visual Studio): Visual Studio Community 2022
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
@vincentremond, there’s an older suggestion and an RFC with implementation in progress, which aim to allow arithmetics (a set of constant expressions) in literals and attributes. It’s sort of a prerequisite for what is requested here. I’m sure enums will then follow sooner rather than later.
Hello, I am not finding the related issue, has it been created ? thanks