Stuart Breckenridge

A Question on Nested Enums

I have a feeling the answer to this little experiment is no, but I thought I’d ask about it anyway.

Take the following example of a nested enumeration of commissioned and non-commissioned Army ranks:

enum ArmyRanks {
    enum Commissioned {
        case SecondLieutenant, Lieutenant, Captain
    }
    
    enum NonCommissioned {
        case Private, LanceCorporal, Corporal
    }
}

If I want to create a struct of a soldier using the above enum to denote rank, I could do this:

struct Soldier {
    let rank:ArmyRanks.Commissioned
}


Or I could do this:

struct Soldier {
    let rank:ArmyRanks.NonCommissioned
}


I’d like to be able to write the struct referencing only ArmyRanks and then be able to provide .Commissioned, or .NonCommissioned as needed.

struct Soldier {
    let rank:ArmyRanks
}


Is this possible?


Supported by