Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named integer literals are not allowed in enum type definition #12896

Closed
bmitc opened this issue Mar 30, 2022 · 5 comments
Closed

Named integer literals are not allowed in enum type definition #12896

bmitc opened this issue Mar 30, 2022 · 5 comments

Comments

@bmitc
Copy link
Contributor

bmitc commented Mar 30, 2022

Repro 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
@bmitc bmitc added the Bug label Mar 30, 2022
@dsyme
Copy link
Contributor

dsyme commented Mar 30, 2022

Please add this to https://github.com/fsharp/fslang-suggestions. We can mark it as approved-in-principle. However note the natural completion is to allow 1 + 2 atc and all computable constant expressions (like this used in attributes) - I think this is feasible but we'd need to check.

@dsyme dsyme added Feature Request and removed Bug labels Mar 30, 2022
@dsyme
Copy link
Contributor

dsyme commented Mar 30, 2022

Closing as should be tracked by a language suggestion

@dsyme dsyme closed this as completed Mar 30, 2022
@bmitc
Copy link
Contributor Author

bmitc commented Mar 30, 2022

Thanks for confirming that this is expected behavior! I will file a feature request there shortly and will link it here.

Thanks!

@vincentremond
Copy link

Hello,
I am not finding the related issue, has it been created ?
thanks

@kerams
Copy link
Contributor

kerams commented Nov 24, 2022

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants