Broadcast comparison of array of enum objects gives an error

pjgorski
Tue, 14 Sep 2021 12:48:34 +0000
Performance

I have an array of @Enum objects. I want to count the array elements having a certain value.

@enum E x = 0 y = 1
Es = [E(0) for i in 1:100]
sum(Es .== E(0))

Above code and just Es .== E(0) gives the following error:

ERROR: MethodError: no method matching length(::E)
Closest candidates are:
length(::Core.SimpleVector) at essentials.jl:582
length(::Base.MethodList) at reflection.jl:732
length(::Core.MethodTable) at reflection.jl:806

I know that I can count elements using for instance
sum([e == E(0) for e in Es])
but I wonder what’s wrong in the 1st approach.

12 posts - 5 participants