[swift-evolution] List of all Enum values (for simple enums)

Zef Houssney zefmail at gmail.com
Fri Dec 11 19:49:53 CST 2015


Yeah thank you —I did mean the constructor, didn’t think of that terminology :)

I recognize that it’s not totally ideal but I do still see that as being useful. Here’s an example of how you could use that. For cases that have the same signature, it’s less ideal but still totally workable as far as I can see.

Also with this approach you could fairly easily create the data structure you suggested yourself.


enum Foo {
    case Plain
    case AssociatedSimple(String)
    case AssociatedComplex(String, Int)
    case AssociatedSimpleCopy(String)

    static var cases: [Any] {
        return [
            Plain,
            AssociatedSimple,
            AssociatedComplex,
            AssociatedSimpleCopy,
        ]
    }
}

let cases = Foo.cases

cases[0] is Foo // true
cases[1] is Foo // false
cases[2] is Foo // false

cases[0] is (String) -> Foo // false
cases[1] is (String) -> Foo // true
cases[2] is (String) -> Foo // false

cases[0] is (String, Int) -> Foo // false
cases[1] is (String, Int) -> Foo // false
cases[2] is (String, Int) -> Foo // true

if let simple = cases[1] as? (String) -> Foo {
    let created = simple("some value")

    switch created {
    case .AssociatedSimple:
        // we'd hit this:
        print("Simple")
    case .AssociatedSimpleCopy:
        // here if you didn't want the original "some value"
        // you could create the enum with a different value
        print("Simple Copy")
    default:
        print("Something else")
    }
}

I don’t love that it’s using [Any] here, but I don’t know if it’s possible to represent that `() -> Foo` where the methods passed can be dynamic… didn’t seem to be with what I was trying. Related to that, it might make more sense to have the “Plain” value above represented as `() -> Foo` instead of simply `Foo`.




> On Dec 11, 2015, at 5:43 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
>> However, I do have a suggestion that’s different from what Brent suggested, which is that instead of returning actual enum values, I could see returning the functions associated with them as being potentially useful. I can show an example of what this might look like if anyone is interested.
> 
> Interesting. The problem I see with this is that, if by “the functions associated with them” you mean their constructors, each constructor would have a different set of parameters. To distinguish between them, you would probably need…an enum.
> 
> On the other hand, if you mean “functions testing an instance of the enum against that case, and returning true if it matches”, that’s a more interesting possibility.
> 
> -- 
> Brent Royal-Gordon
> Architechies
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/17f06b71/attachment.html>


More information about the swift-evolution mailing list