1

Say I have this enum in Ceylon:

abstract class Suit()
        of hearts | diamonds | clubs | spades {}

object hearts extends Suit() {}
object diamonds extends Suit() {}
object clubs extends Suit() {}
object spades extends Suit() {}

I would like to iterate over the values. In Java, I would do Suit.values(). Is there something similar in Ceylon?

I tried to define it my self, but that didn't work because there are no static members in Ceylon? Should I just define at the top level:

Suit[4] suites= [hearts, diamonds, clubs, spades];
drhagen
  • 8,331
  • 8
  • 53
  • 82
  • 1
    http://stackoverflow.com/questions/19402031/ceylon-iterate-enumerated-instances – gdejohn Jan 18 '16 at 21:10
  • I am at a loss as to how that question did not come up in my searches as it is as duplicate as any question could possibly be. Embarrassingly identical... – drhagen Jan 18 '16 at 21:51

1 Answers1

2
for (suit in `Suit`.caseValues) {
    // ...
}

Try online

Note: since this uses the metamodel, it’s somewhat slow – certainly not as fast as Enum.values(). Cache the result if you’re going to use it a lot.

Lucas Werkmeister
  • 2,584
  • 1
  • 17
  • 31