Skip to content

Static method to get array of all enum instances #18

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

Closed
chriseskow opened this issue May 18, 2015 · 5 comments
Closed

Static method to get array of all enum instances #18

chriseskow opened this issue May 18, 2015 · 5 comments

Comments

@chriseskow
Copy link
Contributor

When using this library, I was slightly surprised to find that the toArray() method returns an associative array mapping the enum constant names to their underlying values, rather than to instances of the enum object wrapping the value. When adding additional behavior to the enum via instance methods, having the enum objects is often more useful than only the underlying constant values.

What do you think of adding a static method, e.g. getInstances(), returning an array mapping constant names to object instances?

@mnapoli
Copy link
Member

mnapoli commented May 18, 2015

Hi, this is indeed something I've never thought of, and never needed. But it makes sense, I can imagine plenty of scenarios where you would want to get the list of the actual enum instances…

I think if you make a PR I'd be +1 to merge it, if anybody else has a preference please voice it.

And yes, now I wish toArray() would return the instance list ;) But we have to keep BC. However we have keys(), and a few month ago someone suggested to rename toArray() to values(): we didn't because of BC issues, but we could name the new method values() it would make sense I think.

@chriseskow
Copy link
Contributor Author

We could break BC and release it as 2.0... ;-)

@mnapoli
Copy link
Member

mnapoli commented May 18, 2015

No that's not the goal. As I said if the new method name is values() then there is no BC break.

mnapoli added a commit that referenced this issue May 19, 2015
Add static values() method to return array of all Enum instances (fixes #18)
@mnapoli
Copy link
Member

mnapoli commented May 19, 2015

I have tagged v1.4.0

@chriseskow
Copy link
Contributor Author

Thanks!

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

2 participants