Skip to content

Commit 54cee1f

Browse files
committed
Merge pull request #11568 from JuliaLang/jb/enuminstances
RFC: use an `instances` function for enums
2 parents a9915ff + 06cda80 commit 54cee1f

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

base/Enums.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ macro enum(T,syms...)
9494
end
9595
Base.typemin(x::Type{$(esc(typename))}) = $(esc(typename))($lo)
9696
Base.typemax(x::Type{$(esc(typename))}) = $(esc(typename))($hi)
97-
Base.length(x::Type{$(esc(typename))}) = $(length(vals))
98-
Base.start(::Type{$(esc(typename))}) = 1
99-
Base.next(x::Type{$(esc(typename))},s) = ($(esc(typename))($values[s]),s+1)
100-
Base.done(x::Type{$(esc(typename))},s) = s > $(length(values))
101-
Base.names(x::Type{$(esc(typename))}) = [$(map(x->Expr(:quote, (x[1])), vals)...)]
10297
Base.isless(x::$(esc(typename)), y::$(esc(typename))) = isless(x.val, y.val)
98+
let insts = ntuple(i->$(esc(typename))($values[i]), $(length(vals)))
99+
Base.instances(::Type{$(esc(typename))}) = insts
100+
end
103101
function Base.print(io::IO,x::$(esc(typename)))
104102
for (sym, i) in $vals
105103
if i == x.val

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,7 @@ export
10471047
promote_rule,
10481048
promote_type,
10491049
subtypes,
1050+
instances,
10501051
super,
10511052
typeintersect,
10521053
typejoin,

base/reflection.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ end
8181
type_alignment(x::DataType) = ccall(:jl_get_alignment,Csize_t,(Any,),x)
8282
field_offset(x::DataType,idx) = ccall(:jl_get_field_offset,Csize_t,(Any,Int32),x,idx)
8383

84+
# return all instances, for types that can be enumerated
85+
function instances end
86+
8487
# subtypes
8588
function _subtypes(m::Module, x::DataType, sts=Set(), visited=Set())
8689
push!(visited, m)

doc/stdlib/base.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ Types
482482
julia> f(apple)
483483
"I'm a FRUIT with value: 1"
484484

485+
.. function:: instances(T::Type)
486+
487+
Return a collection of all instances of the given type, if applicable.
488+
Mostly used for enumerated types (see ``@enum``).
489+
485490
Generic Functions
486491
-----------------
487492

test/enums.jl

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ using Base.Test
2727
@test Fruit(0x00) == apple
2828
@test Fruit(big(0)) == apple
2929
@test_throws MethodError Fruit(0.0)
30-
@test start(Fruit) == 1
31-
@test next(Fruit,1) == (apple,2)
32-
@test next(Fruit,2) == (orange,3)
33-
@test next(Fruit,3) == (kiwi,4)
34-
@test !done(Fruit,3)
35-
@test done(Fruit,4)
36-
@test length(Fruit) == 3
3730
@test typemin(Fruit) == apple
3831
@test typemax(Fruit) == kiwi
3932
@test convert(Fruit,0) == apple
@@ -49,7 +42,7 @@ using Base.Test
4942
@test convert(Bool,apple) == false
5043
@test convert(Bool,orange) == true
5144
@test_throws InexactError convert(Bool,kiwi)
52-
@test names(Fruit) == [:apple, :orange, :kiwi]
45+
@test instances(Fruit) == (apple, orange, kiwi)
5346

5447
f(x::Fruit) = "hey, I'm a Fruit"
5548
@test f(apple) == "hey, I'm a Fruit"
@@ -59,12 +52,12 @@ d = Dict(apple=>"apple",orange=>"orange",kiwi=>"kiwi")
5952
@test d[orange] == "orange"
6053
@test d[kiwi] == "kiwi"
6154
vals = [apple,orange,kiwi]
62-
for (i,enum) in enumerate(Fruit)
55+
for (i,enum) in enumerate(instances(Fruit))
6356
@test enum == vals[i]
6457
end
6558

6659
@enum(QualityofFrenchFood, ReallyGood)
67-
@test length(QualityofFrenchFood) == 1
60+
@test length(instances(QualityofFrenchFood)) == 1
6861
@test typeof(ReallyGood) <: QualityofFrenchFood <: Enum
6962
@test Int(ReallyGood) == 0
7063

@@ -95,7 +88,7 @@ end
9588
@enum Test3 _one_Test3=0x01 _two_Test3=0x02 _three_Test3=0x03
9689
@test typeof(_one_Test3.val) <: UInt8
9790
@test _one_Test3.val === 0x01
98-
@test length(Test3) == 3
91+
@test length(instances(Test3)) == 3
9992

10093
@enum Test4 _one_Test4=0x01 _two_Test4=0x0002 _three_Test4=0x03
10194
@test _one_Test4.val === 0x0001

0 commit comments

Comments
 (0)