Why no non-integral enums?

后端 未结 4 1175
挽巷
挽巷 2021-01-18 09:20

Why is it that non-integral enums cannot be created? I want to know if this is a language design decision, or if there are issues with implementing this in the compiler.

4条回答
  •  萌比男神i
    2021-01-18 09:48

    Without knowing the actual answer, I'd suggest that it's more the latter - there's no real justifiable need.

    Enums in my mind are simply there to represent the various options available in a particular case; not to actually store data.

    A variable holding an enum value shouldn't be thought of as holding the value 1 or 2 or 3, even if those are the actual values stored in that memory location. It should be thought of as representing Monday, Tuesday or Wednesday (for example) instead. It only has meaning in terms of that enumeration and really shouldn't (although often does) be used in other ways. Operations you perform on Enum values are simply programming shortcuts made available by the way Enums have been implemented.

提交回复
热议问题