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

Add Enum.valid? #5716

Merged
merged 6 commits into from Nov 8, 2019
Merged

Conversation

makenowjust
Copy link
Contributor

As the first step to establish the exhaustiveness checking for enums, I want to define what enum value is valid or invalid. In this PR, a valid enum value means that ThisEnum.from_value?(value) returns the ThisEnum member, and an invalid enum value means that it returns nil. It makes sense to me, but maybe it is more natural if the relation between from_value? and valid? is reversed.

The reason that this is implemented as a class method is explained in NOTE section of the document comment.

@RX14
Copy link
Contributor

RX14 commented Feb 13, 2018

I'm fine with this, but obviously we can't rely on this method in Codegen.

@makenowjust
Copy link
Contributor Author

I don't intend to use this method in codegen entirely. But this method provides the way to distinguish an invalid (undefined) value from unhandled values in else block of case.

src/enum.cr Outdated
# an instance method `valid?` is defined by the language when a user
# defines an enum member named `Valid`.
def self.valid?(value : self) : Bool
from_value?(value.to_i) ? true : false
Copy link
Contributor

@Sija Sija Feb 13, 2018

Choose a reason for hiding this comment

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

you could skip ternary expression: !!from_value?(value.to_i) yields the same result.

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 is preferred. Thank you.

@makenowjust makenowjust force-pushed the feature/enum/valid branch 2 times, most recently from b237e1f to 5fbebe9 Compare February 14, 2018 13:45
@bew
Copy link
Contributor

bew commented Jun 4, 2018

Is it still useful for "the exhaustiveness checking for enums" ?

@jkthorne
Copy link
Contributor

jkthorne commented Sep 4, 2018

@makenowjust what is the status on this?

@jkthorne
Copy link
Contributor

Is this just waiting for a review?

spec/std/enum_spec.cr Outdated Show resolved Hide resolved
spec/std/enum_spec.cr Outdated Show resolved Hide resolved
src/enum.cr Outdated Show resolved Hide resolved
@straight-shoota straight-shoota added kind:feature topic:stdlib pr:needs-work A PR requires modifications by the author. labels Feb 11, 2019
@bararchy
Copy link
Contributor

If @makenowjust isn't working on this, it should be adopted or closed for the sake of cleaning up

@makenowjust
Copy link
Contributor Author

@barachy I will tackle with this issue tomorrow. This day is free to me. Sorry for slow resoponse 🙇‍♂️

@j8r
Copy link
Contributor

j8r commented Oct 10, 2019

It could be an instance method if the ? is removed. It would be too bad not to have one.

@makenowjust
Copy link
Contributor Author

Rebase to master and resolve request changes are done.

src/enum.cr Outdated Show resolved Hide resolved
src/enum.cr Show resolved Hide resolved
src/enum.cr Outdated Show resolved Hide resolved
@RX14 RX14 added this to the 0.32.0 milestone Oct 23, 2019
SpecEnum.valid?(SpecEnum.new(3i8)).should be_false
end

it "for flags enum" do
Copy link
Member

Choose a reason for hiding this comment

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

should the following be added here?

SpecEnumFlags.valid?(SpecEnumFlags::None).should be_true

There is an existing spec for SpecEnumFlags.from_value(0).should eq(SpecEnumFlags::None).

Other than that, is GTG

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also added SpecEnumFlags::All case.

makenowjust and others added 5 commits October 24, 2019 09:29
A valid enum value means that `ThisEnum.from_value?(value)` returns the
`ThisEnum` member, and an invalid enum value means that it returns
`nil`.
This is implemented as the such way.

The reason that this is implemented as a class method is explained
in NOTE section of the document comment.
Co-Authored-By: Johannes Müller <johannes.mueller@smj-fulda.org>
Co-Authored-By: Johannes Müller <johannes.mueller@smj-fulda.org>
@bcardiff bcardiff removed the pr:needs-work A PR requires modifications by the author. label Nov 8, 2019
@bcardiff bcardiff merged commit 98fc639 into crystal-lang:master Nov 8, 2019
@makenowjust makenowjust deleted the feature/enum/valid branch November 8, 2019 16:01
didactic-drunk pushed a commit to didactic-drunk/crystal that referenced this pull request Nov 8, 2019
A valid enum value means that `ThisEnum.from_value?(value)` returns the
`ThisEnum` member, and an invalid enum value means that it returns
`nil`.
This is implemented as the such way.

The reason that this is implemented as a class method is explained
in NOTE section of the document comment.
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