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

Enumerated types #105

Open
timdp opened this issue Nov 30, 2014 · 10 comments
Open

Enumerated types #105

timdp opened this issue Nov 30, 2014 · 10 comments

Comments

@timdp
Copy link

timdp commented Nov 30, 2014

I just came across a case where I could've used an enum rather than a bunch of constants. Not saying Spider should necessarily have them, but it's worth discussing.

@alongubkin
Copy link
Owner

Proposal

enum WeekDay {
  sunday = 1,
  monday = 2,
  tuesday = 3,
  wednesday = 4,
  thursday = 5,
  friday = 6,
  saturday = 7
}

var day = WeekDay.sunday;
console.log(day); // => 7

enum Color {
  red, blue, green
}

console.log(Color.red); // => "red"

@Namek
Copy link
Collaborator

Namek commented Nov 30, 2014

enum UnitType {
  Unknown = 0,
  Worker = 3
  Soldier, // automatically: = 4
  Car // 5
}

@PhiLhoSoft
Copy link

Dart got them recently, in a version simpler than Java (which can associate any arbitrary data to an enum value), but still useful.
Don't forget that enums should be enumerable... Ie. we should be able to loop on the values.
Of course, we can switch on them, too, perhaps with check that all values are covered in the switch.
Can be useful to have two ways to get information from an enum value: its value, and its name.

@alongubkin
Copy link
Owner

You can loop the values with:

for value of Color { ... }

@Skalman
Copy link

Skalman commented Nov 30, 2014

Enums might as well reuse the object syntax:

enum WeekDay {
  sunday: 1,
  monday: 2,
  tuesday: 3,
  wednesday: 4,
  thursday: 5,
  friday: 6,
  saturday: 7,
}

What's the advantage of using an enum over a regular object? That it's immutable?

@alongubkin
Copy link
Owner

@Skalman We can use Object.freeze to make them immutable.

@Namek
Copy link
Collaborator

Namek commented Nov 30, 2014

@Skalman look at my example above. Imagine enum with 100 values. I'd like to have them automatically increased.

@Skalman
Copy link

Skalman commented Nov 30, 2014

@Namek, I meant object-like syntax, i.e. using : instead of =. @alongubkin's Color example would work without modification.

I also prefer @alongubkin's version, where values are strings, not numbers (Color.red == "red", not Color.red == 0)

@Namek
Copy link
Collaborator

Namek commented Dec 1, 2014

I also prefer @alongubkin's version, where values are strings, not numbers

@Skalman It's not about a preference. Look at first post once again, both are possible. How do you even use enums not wanting integers in there?

@nmn
Copy link

nmn commented Dec 1, 2014

I think enums aren't very useful in a language without type checking. A simple object works just as well in Spider.

In swift enums are useful, as you can use .value syntax and swift already knows which enum you want the value from.

Obviously this feature has no obvious downsides, but it makes sense to keep Spider simple, and avoid features of little value.

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

No branches or pull requests

6 participants