Skip to content

Commit

Permalink
Add Enum.new(self) for auto-casting in macro-generated code (#8634)
Browse files Browse the repository at this point in the history
* Allow to create an enum from a macro symbol

* Rename method and simplify doc

* Simplify spec
  • Loading branch information
bew authored and RX14 committed Dec 30, 2019
1 parent 0b7516c commit 633116c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions spec/std/enum_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ describe Enum do
end
end

it "creates an enum instance from an auto-casted symbol (#8573)" do
enum_value = SpecEnum.new(:two)
enum_value.should eq SpecEnum::Two
end

it "gets value" do
SpecEnum::Two.value.should eq(1)
SpecEnum::Two.value.should be_a(Int8)
Expand Down
5 changes: 5 additions & 0 deletions src/enum.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
struct Enum
include Comparable(self)

# Returns *value*.
def self.new(value : self)
value
end

# Appends a `String` representation of this enum member to the given *io*.
#
# See also: `to_s`.
Expand Down

0 comments on commit 633116c

Please sign in to comment.