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

Allow explicit enum values #114

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

harrisi
Copy link
Contributor

@harrisi harrisi commented Sep 27, 2024

This PR adds the ability to set values for enums, so this type of C code can be generated:

enum Foo {
  FOO_A = 1,
  FOO_B,
  FOO_C = 4
};

The syntax I went with is the following:

type foo :: {:a, 1} | :b | {:c, 4}

The only real alternative I could think of would be something like this:

type foo :: :a = 1 | :b | :c = 4

I don't like this, since it's nonsensical in normal Elixir.

I originally thought a list would be okay, something like this:

type foo :: [a: 1, b: 2, c: 3]

But this doesn't allow for partially explicit definitions, unless all the explicit definitions are at the end (to make it a valid keyword list), so the above definition isn't possible:

type foo :: [a: 1, :b, c: 3] # this doesn't work

I think the desugared pseudo-keyword list is a decent option. It seems less weird than writing :a = 1 to me, anyway. It could also be defined as a list (e.g., [:a, 1] | :b), but that seems confusing since [type] is meaningful elsewhere.

@mat-hek
Copy link
Member

mat-hek commented Sep 30, 2024

👋 thanks for the PR 😉 Personally I find quite unobvious that the alternative of tuples maps to an enum 🤔 What about

@type foo :: enum(a: 1, b: 2)
@type foo :: enum({:a, 1} | {:b, 2})
@type foo :: enum_value(:a, 1) | enum_value(:b, 2)

I like the last one the most, except that enum_value is extremely long. WDYT @FelonEkonom?

@harrisi
Copy link
Contributor Author

harrisi commented Sep 30, 2024

I agree it's not the best syntax, so I'm happy to see more suggestions.

Something all three of your suggestions highlight is the use of the word "enum". I think it would be helpful to change the spec dsl to use enum instead of type in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New PRs by community
Development

Successfully merging this pull request may close these issues.

2 participants