I use enums to store string values like this:
enum Animals: String { case descCat = "I has attitude" case descDog = "how can I help" case descGator = "I will eat you" var s: String { get { return self.rawValue as String } } }
Then I access them like this:
print("Dogs be like:" + Animals.descDog.s)
My question is can I extend enums like any other struct or object so I don't have to add the var s: String {}
property to each enum?
Enum and has several static members. Therefore enum cannot extend any other class or enum: there is no multiple inheritance. Class cannot extend enum as well. This limitation is enforced by compiler.
Enum cannot extend any class in java,the reason is, by default Enum extends abstract base class java. lang. Enum. Since java does not support multiple inheritance for classes, Enum can not extend another class.
The reason you can't extend Enums is because it would lead to problems with polymorphism.
Can you extend enums? The short answer is no, you can't extend enums because TypeScript offers no language feature to extend them.
You want to add a property to all enums whose raw value is a string? This sounds like a case for constrained protocol extensions!
extension RawRepresentable where RawValue == String { var description: String { return rawValue } }
This works because all enums with a raw value automatically conform to the RawRepresentable
protocol, and said protocol has an associated type RawValue
that tells you which type the raw value is.
Now your Animals
enum will automatically inherit it:
print(Animals.descCat.description) // -> "I has attitude"
Notice that string enums are themselves already CustomStringConvertible
, so they already have a description
property (that returns the name of the enum case), and yours doesn't override it:
print(Animals.descCat) // -> "descCat"
If you want your description
to override the default, just add a declaration of CustomStringConvertible
conformance to your enum:
private enum Animals: String, CustomStringConvertible { /*...*/ } print(Animals.descCat) // -> "I has attitude"
You can also extend this idea to cover other raw value types. For example:
extension RawRepresentable where RawValue: CustomStringConvertible { var description: String { return rawValue.description } }
Now, you can get automatic descriptions for enums whose raw value is Int
or even a custom type (so long as that type has a description
of its own).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With