Open KCreate opened 7 years ago
@KCreate Good idea, that gives us more opportunities to seperate the functions from other objects
@KCreate But are the functions actually necessary? Any examples for a good use?
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
}
}
Enums can also have static methods
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: