coco icon indicating copy to clipboard operation
coco copied to clipboard

`enum`

Open satyr opened this issue 12 years ago • 6 comments

What should it do?

satyr avatar Jul 21 '12 11:07 satyr

I was thinking about this to. Potentially define a constant (and frozen) ES6 set?

gkz avatar Jul 22 '12 02:07 gkz

You could have it compile to inline constants and leave the enum out of the compiled code. However if the enum was ever exported, which I think would the the more common use case, you'd have to render the enum as an object.

andrewrk avatar Jul 22 '12 04:07 andrewrk

Actually an object makes more sense than a set, you're right.

gkz avatar Jul 22 '12 05:07 gkz

It seems like the value of this as a language feature would be limited, and as such I would opt to leave it out of coco in the interest of keeping the language smaller. Just my 2 cents.

andrewrk avatar Jul 22 '12 05:07 andrewrk

Typically, the assumption with enums is that they are defined seperately and identically in all the places they are used--actually, that's kind of the point. If you do implement them, I would favor a semantics similar to C; assign elements the natural numbers in monotonically increasing order for all elements, allowing the users to explicitly specify values--reserving them from the set of naturals from which you draw from for the unassigned elements as necessary--for certain elements of the enum. Clearly you are free to support several assignment behaviors if you wish, e.g. attempting to assign a unique bit to each element by preceding with powers of two.

In order to support interoperability with raw JS, generated code readability, exportability, etc., these values should be turned into an object, e.g.

Colors = enum Red, Green, Blue

becomes

var Colors = {
  Red: 1,
  Green: 2,
  Blue: 3
};

I'm greatly in favor of keeping the language small, but we've already got classes and this falls within the same train of thought. Let's either have both or neither.

ksdlck avatar Oct 11 '12 18:10 ksdlck

FWIW, I'd forego the inlining of constants on compilation. Maybe do it if the enum is defined in the same file, but the JIT optimizer on whatever platform you're using should take care of this. Inlining anywhere else causes more problems than it's worth, and even within the same file reduces code readability.

ksdlck avatar Oct 11 '12 18:10 ksdlck