Crystal: What to do with negative enum flags values

Created on 17 Apr 2018  路  3Comments  路  Source: crystal-lang/crystal

While doing #5954 I noticed this behavior:

@[Flags]
enum Foo
  Bar = -2
  Baz
end

pp Foo::Bar.value # -2
pp Foo::Baz.value # -4

Is it intended? Or should this be an error (for flag enum at least) ?

If it should be an error, maybe we should also restrict enum flags to unsiged integer type (and default to UInt32 instead of Int32)

bug compiler

Most helpful comment

@Sija we are talking about enum flags here, not generic constants

All 3 comments

Maybe we can restrict them to being positive or zero.

@asterite I object. There are legitimate cases of constants enum members with minus values. See for instance http://gphoto.org/doc/api/gphoto2-result_8h.html.

@Sija we are talking about enum flags here, not generic constants

Was this page helpful?
0 / 5 - 0 ratings