3 ms·
I see Swift supports generics so could they let you define subclasses of string enums that support custom string values?
by lozenge 9y ago
I see Swift supports generics so could they let you define subclasses of string enums that support custom string values?
- favorited 9y agoSwift also supports associated values with enum cases, so you could do something like this[0] and get type-safety. Now you could only associate a URL type with a link attribute, or an NSColor type with a foregroundColor attribute, etc. However, you can't (at least currently) add new cases to someone else's enum (the way you can add methods). So if I decided I wanted my own custom text attributes (which isn't uncommon), we're back to type-unsafe land. We'd need the original enum to have an extra attribute case, like this[1]. [0]https://pastebin.com/L1xPcueu https://pastebin.com/L1xPcueu [1]https://pastebin.com/h8Rp3Ajy https://pastebin.com/h8Rp3Ajy