question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Named integer literals are not allowed in enum type definition

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
keramscommented, 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.

0reactions
vincentremondcommented, Nov 24, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enum values from non-integer literals?
No. enums are constants with type int.
Read more >
Why are two literals allowed to have the same values in ...
Enumerations are just named integer constants with a type, so you can have two constants with the same name. Notably this allows you...
Read more >
Literal types and Enums - mypy 1.5.1 documentation
Literal types may contain one or more literal bools, ints, strs, bytes, and enum values. However, literal types cannot contain arbitrary expressions: types...
Read more >
TypeScript enums vs. types: Enhancing code readability
Enums allow us to define or declare a collection of related values that can be numbers or strings as a set of named...
Read more >
TypeScript: Handbook - Enums
Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found