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 redefining None to 0 for @[Flags] enum #6160

Merged

Conversation

bew
Copy link
Contributor

@bew bew commented Jun 4, 2018

Followup on #4395 (comment)

#4395 now prevents All & None in @[Flags] enums.

But as @oprypin pointed out / demonstrated, redefining None & All can be useful for documentation purpose.

To this end:

it should be possible to redefine None and All but only if their value matches what they would have been anyway.

None should always be 0, and All should always be all the values OR-ed together.

So something like this should work:

@[Flags]
enum Foo
  None = 0
  A
  B
  C
  All = A | B | C
end

It's easy to handle None, just need to check it's 0.
About All, first it's very tedious to repeat all members, second it's harder to detect if there are more members after it. To simplify All and still be able to documente it, there could be a special value like All = :all that will skip this member but retain its doc somehow...


As handling None is easy I implemented it in this PR.
Also, I don't think All is common when using enum flags, so I think it's ok to not be able to document it.

WDYT?

@jhass
Copy link
Member

jhass commented Jun 4, 2018

In general I'm +1 to this, not sure I like :all though. Can we allow a self-reference here? Something like All = All or All = Foo::All

@oprypin
Copy link
Member

oprypin commented Jun 4, 2018

Mentioning :all was a bad call because it's not even part of this PR 🐱.

I would be very happy to have just None merged.

@bew
Copy link
Contributor Author

bew commented Jun 4, 2018

Yeah I know @oprypin I just wanted to add some context, and I needed to mention All for completeness 😃

@bew
Copy link
Contributor Author

bew commented Jun 4, 2018

I just added a better empty enum check that ignores None & All in flags enums, now that they are allowed

@@ -569,7 +569,13 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
is_flags: enum_type.flags?)
end

if enum_type.types.empty?
nb_members = enum_type.types.size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's nb_members supposed to mean? I'd suggest to use a more descriptive name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's the number of members, maybe number_of_members then? it just felt too long ^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no "too long" for names, only "too short" 😄 num_members would be fine, tough. Or members_size, enum_size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

num_members is good 😃 ty

Copy link
Member

@bcardiff bcardiff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think there is a spec missing regarding how All is ignored. From the PR initial message it wasn't clear if the user needs to A | B | C or not

@oprypin
Copy link
Member

oprypin commented Jun 7, 2018

There is no change in behavior of All.

@bew
Copy link
Contributor Author

bew commented Jun 7, 2018

Yeah sorry if it wasn't clear @bcardiff, the title only mention the possibility to redefine None, not All. The PR inital msg is providing context and explanations, maybe it should have gone in an issue.

@RX14 RX14 added this to the 0.25.0 milestone Jun 9, 2018
@RX14 RX14 merged commit 44ff90e into crystal-lang:master Jun 9, 2018
@bew
Copy link
Contributor Author

bew commented Jun 10, 2018

Thank you!

@bew bew deleted the allow-redefine-None-to-0-in-enum-flags branch June 10, 2018 02:46
@straight-shoota
Copy link
Member

No, thank you, @bew =)

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

Successfully merging this pull request may close these issues.

None yet

6 participants