Lua Notes

enums requiring a first value
Login

enums requiring a first value

From the overview:

local Weeks = @enum{
  Sunday = 0,
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday
}
print(Weeks.Sunday) -- outputs: 0

local a: Weeks = Weeks.Monday
print(a) -- outputs: 1

Most languages' enums don't require that = 0, and would implicitly give these enums the same values as Nelua does here.

Walter Bright remarks

And BTW,
enum { Yes, No };

is just an automatic “no hire” decision, as if (Yes) will thoroughly confuse everyone. If you’ve done this, run and fix it before someone curses your entire ancestry.

Why would someone ever do that? With implied numerical values, the problem might've just been overlooked.

From discussion:

His point here was not that having an enum with values for yes and no is a bad idea. The bad idea is assigning yes the value 0. That's a much more subtle point, as here nowhere 0 is written. That's what make this kind of mistakes so easy and I think it's well worth to explain it to less experienced programmers.

It's not easy to make simple looking code. That has nothing to do with (coding-)style, it is all about not defining things a different way than it is usually done, so nobody mix it up. Sometimes it's very hard to find the correct order of things - often even experiments are necessary to determine what "usually" means. What he says is: Invest your time to find out what "usually" means, and that is not trivial, even if it sounds like it is. It requires a lot of experience to come to this conclusion. Investing time to make things look easy and obvious is well worth it, despite you're likely not payed (or in your case: honored) for it.