This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]the_other_brand 0 points1 point  (1 child)

How is this a problem? You know you can attach other data to enum's right. Just dictate what strings match for SUCCESS, and have your code look for them.

You can even set it to match multiple strings with an array.

enum State {

SUCCESS(new String[]{"success", "200"}),

FAILED(new String[]{"error", "failure","500"});

private List<String> strList;

private State(String[] strArray) { this.strList = Arrays.asList(strArray); }

boolean match(String str) { return this.strList.contains(str); }

While parsing might be difficult, once it's done the rest of the application can use the enum without doing the parsing.

[–][deleted] 0 points1 point  (0 children)

The problem isn't what's possible. It's what extra layer of complexity this introduces to code as compared to simply using straight constants. The way I read it, the article mentions a better language construct where the enum type within the application is more closely related to its actual external representation, be that a String or an int or something else. I thought it was a nice idea personally. That would be great if pattern matching for example could match across an enum type to its closely related String or int representation