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

Enums #120

Open
KCreate opened this issue Dec 18, 2016 · 3 comments
Open

Enums #120

KCreate opened this issue Dec 18, 2016 · 3 comments
Labels

Comments

@KCreate
Copy link
Collaborator

KCreate commented Dec 18, 2016

enum MyEnum {
  Foo
  Bar
  Baz
}

MyEnum.Foo # => 0
MyEnum.Bar # => 1
MyEnum.Baz # => 2

Enums can also have static methods

enum MyEnum {
  Foo
  Bar
  Baz

  func is_foo(value) {
    value == @Foo
  }
}

The values of an enum are Numeric values starting at 0, incrementing with each value added to the enum. The enum is represented as an object.

The above code is equivalent to the following object literal:

let MyEnum = {
  const Foo = 0
  const Bar = 1
  const Baz = 2

  func is_foo(value) {
    value == @Foo
  }
}
@KCreate KCreate added the Syntax label Dec 18, 2016
@ghost
Copy link

ghost commented Apr 22, 2017

@KCreate Good idea, that gives us more opportunities to seperate the functions from other objects

@ghost
Copy link

ghost commented Dec 12, 2017

@KCreate But are the functions actually necessary? Any examples for a good use?

@KCreate
Copy link
Collaborator Author

KCreate commented Dec 12, 2017

Writing code with enums usually requires comparing them every so often.
Sometimes that code becomes long and complicated. Allowing functions directly inside enums
makes it possible to encapsulate logic and abstract implementation details away.

En example that comes to mind is checking whether a type of token is a keyword or not.

enum Token {
  Let
  Const
  If
  Integer

  func is_keyword() {
    return self == Token.Let || self == Token.Const
  }
}

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

No branches or pull requests

1 participant