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

Inline enum values #1819

Open
lerno opened this issue Jan 12, 2025 · 7 comments
Open

Inline enum values #1819

lerno opened this issue Jan 12, 2025 · 7 comments
Labels
Discussion needed This feature needs discussion to iron out details Enhancement Request New feature or request Loose idea Probably will not be included in the language, but documented to allow discussion

Comments

@lerno
Copy link
Collaborator

lerno commented Jan 12, 2025

Possible feature:

enum Foo : inline int
{
   ABC,
   DEF,
   GHJ
}
...
int a = Foo.GHJ; // a is 2

enum Bar : int (inline String name)
{
  XYY = "a",
  XXZ = "b",
  YZZ = "c"
}  
...
String foo = Bar.YZZ; // foo is "c"
@lerno lerno added the Enhancement Request New feature or request label Jan 12, 2025
@lerno
Copy link
Collaborator Author

lerno commented Jan 12, 2025

Do we want this?

@lerno
Copy link
Collaborator Author

lerno commented Jan 12, 2025

If this is used for C enums, then this would not mean you can do:

enum Foo : { inline int val }
{
   ABC = 4;
 }
 extern void useFoo(Foo f);
 ...
 useFoo(ABC);

Instead you'd have to do

enum Foo : { inline int val }
{
   ABC = 4;
 }
extern void useFoo(int val);
 ...
useFoo(Foo.ABC);

So all it saves is the Foo.ABC.val

@lerno lerno added the Discussion needed This feature needs discussion to iron out details label Jan 12, 2025
@vssukharev
Copy link
Contributor

I love inlined associated values but, as far as I remember, there are some cons of it

@lerno
Copy link
Collaborator Author

lerno commented Jan 12, 2025

Most of those issues are removed when going to from_ordinal ordinal to convert to the ordinal.

@alexveden
Copy link
Contributor

If this is used for C enums, then this would not mean you can do:

Does this mean that enums in C3 are allowed to have gaps, and starting from different order? Duplicate values for different enum items? If it could improve C compatibility, I think it could be beneficial.

@vssukharev
Copy link
Contributor

vssukharev commented Jan 12, 2025

Does this mean that enums in C3 are allowed to have gaps, and starting from different order? Duplicate values for different enum items? If it could improve C compatibility, I think it could be beneficial.

By design, having gaps in enums wouldn't be possible in C3 as they are like bound to associated values.

@lerno
Copy link
Collaborator Author

lerno commented Jan 12, 2025

Does this mean that enums in C3 are allowed to have gaps, and starting from different order? Duplicate values for different enum items? If it could improve C compatibility, I think it could be beneficial.

No but you could treat them a bit as if they were values with gaps by inlining an associated value.

@lerno lerno added the Loose idea Probably will not be included in the language, but documented to allow discussion label Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion needed This feature needs discussion to iron out details Enhancement Request New feature or request Loose idea Probably will not be included in the language, but documented to allow discussion
Projects
None yet
Development

No branches or pull requests

3 participants