Julia: 1-based @enums

Created on 30 Jul 2018  路  7Comments  路  Source: JuliaLang/julia

As julia has 1-based arrays, shouldn't enums also be 1-based? Maybe interoperability with C code weights more more important in this case?

Most helpful comment

Why does this alleged form of "consistency" matter?

It might matter if you could have enums acting as array indexes.

I wanted this functionality and created an IntEnums module (rather crude, non-general, non-production).

Perhaps Enums might support this in the future, in which case compatibility with standard array indexing would be better now rather than possibly breaking changes later.

Of course this might be a bit of a stretch.

Julia-0.6.4> @IntEnum Regions north east south west
IntEnum Regions:
  north = 1
  east = 2
  south = 3
  west = 4

Julia-0.6.4> sales = zeros(Float64, length(Regions))
4-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0

Julia-0.6.4> for region in Regions
                 sales[region] = rand(0:50000)/100
             end

Julia-0.6.4> for region in Regions
                 println(region, " ", sales[region])
             end
north 66.19
east 129.88
south 409.46
west 200.81

All 7 comments

thanks, I see, that this has been discussed extensively, I'll just close this.

You certainly have to question any change that only requires 2 more characters to get the desired behavior:

@Fruit apple banana orange

vs.

@Fruit apple=1 banana orange

the other way round one could argue that it would only take 2 more characters to turn a Julia-consisten (1-based) enum into a C compatible enum (0-based)

@enum Fruit apple banana orange

vs.

@enum Fruit apple=0 banana orange

Why does this alleged form of "consistency" matter?

I don't think it's a big deal.

Why does this alleged form of "consistency" matter?

It might matter if you could have enums acting as array indexes.

I wanted this functionality and created an IntEnums module (rather crude, non-general, non-production).

Perhaps Enums might support this in the future, in which case compatibility with standard array indexing would be better now rather than possibly breaking changes later.

Of course this might be a bit of a stretch.

Julia-0.6.4> @IntEnum Regions north east south west
IntEnum Regions:
  north = 1
  east = 2
  south = 3
  west = 4

Julia-0.6.4> sales = zeros(Float64, length(Regions))
4-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0

Julia-0.6.4> for region in Regions
                 sales[region] = rand(0:50000)/100
             end

Julia-0.6.4> for region in Regions
                 println(region, " ", sales[region])
             end
north 66.19
east 129.88
south 409.46
west 200.81
Was this page helpful?
0 / 5 - 0 ratings

Related issues

omus picture omus  路  3Comments

helgee picture helgee  路  3Comments

tkoolen picture tkoolen  路  3Comments

TotalVerb picture TotalVerb  路  3Comments

Keno picture Keno  路  3Comments