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

1-based @enums #28351

Closed
gdkrmr opened this issue Jul 30, 2018 · 7 comments
Closed

1-based @enums #28351

gdkrmr opened this issue Jul 30, 2018 · 7 comments

Comments

@gdkrmr
Copy link
Contributor

gdkrmr commented Jul 30, 2018

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?

@mauro3
Copy link
Contributor

mauro3 commented Jul 30, 2018

x-ref: #10168 (comment)

@gdkrmr
Copy link
Contributor Author

gdkrmr commented Jul 30, 2018

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

@gdkrmr gdkrmr closed this as completed Jul 30, 2018
@quinnj
Copy link
Member

quinnj commented Jul 30, 2018

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

@gdkrmr
Copy link
Contributor Author

gdkrmr commented Jul 30, 2018

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

@JeffBezanson
Copy link
Sponsor Member

Why does this alleged form of "consistency" matter?

@gdkrmr
Copy link
Contributor Author

gdkrmr commented Jul 30, 2018

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

@GregPlowman
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants