Iterate enumerated class instances

后端 未结 2 830
一向
一向 2021-01-21 01:23

Is there a simple way to iterate over all enumerated instances of a class in Ceylon?

Just like values() for Java enums?

abstract class Suit(         


        
相关标签:
2条回答
  • 2021-01-21 01:31

    This can be done now with ClassOrInterface.caseValues:

    Suit[] suits = `Suit`.caseValues;
    

    Regarding the returned sequence:

    This sequence is ordered and skips any case type to only include case values.

    0 讨论(0)
  • 2021-01-21 01:43

    One way is to use the metamodel:

    for (caseType in `class Suit`.caseTypes) {
        assert (is OpenClassOrInterfaceType caseType);
        print(caseType.declaration.name);
    }
    
    0 讨论(0)
提交回复
热议问题