<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Yeah thank you —I did mean the constructor, didn’t think of that terminology :)<div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">Also with this approach you could fairly easily create the data structure you suggested yourself.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><pre class="rich-diff-level-zero" style="box-sizing: border-box; overflow: auto; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; margin-top: 0px; margin-bottom: 16px; line-height: 1.45; padding: 10px 20px; background-color: rgb(247, 247, 247); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-wrap: normal; margin-left: 15px; color: rgb(51, 51, 51);"><span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">enum</span> Foo {
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> Plain
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> AssociatedSimple(<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>)
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> AssociatedComplex(<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>, <span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>)
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> AssociatedSimpleCopy(<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>)
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> cases: [<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">Any</span>] {
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> [
Plain,
AssociatedSimple,
AssociatedComplex,
AssociatedSimpleCopy,
]
}
}
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> cases <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> Foo<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>cases
cases[<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">0</span>] <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">is</span> Foo <span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// true</span>
cases[<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">1</span>] <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">is</span> Foo <span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// false</span>
cases[<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">2</span>] <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">is</span> Foo <span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// false</span>
cases[<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">0</span>] <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">is</span> (<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>) <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> Foo <span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// false</span>
cases[<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">1</span>] <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">is</span> (<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>) <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> Foo <span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// true</span>
cases[<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">2</span>] <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">is</span> (<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>) <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> Foo <span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// false</span>
cases[<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">0</span>] <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">is</span> (<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>, <span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>) <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> Foo <span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// false</span>
cases[<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">1</span>] <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">is</span> (<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>, <span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>) <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> Foo <span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// false</span>
cases[<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">2</span>] <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">is</span> (<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>, <span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>) <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> Foo <span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// true</span>
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> simple <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> cases[<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">1</span>] <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> (<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>) <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> Foo {
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> created <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> simple(<span class="pl-s rich-diff-level-one" style="box-sizing: border-box; color: rgb(24, 54, 145);"><span class="pl-pds" style="box-sizing: border-box;">"</span>some value<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">switch</span> created {
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>AssociatedSimple:
<span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// we'd hit this:</span>
<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">print</span>(<span class="pl-s rich-diff-level-one" style="box-sizing: border-box; color: rgb(24, 54, 145);"><span class="pl-pds" style="box-sizing: border-box;">"</span>Simple<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">case</span> <span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>AssociatedSimpleCopy:
<span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// here if you didn't want the original "some value"</span>
<span class="pl-c rich-diff-level-one" style="box-sizing: border-box; color: rgb(150, 152, 150);">// you could create the enum with a different value</span>
<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">print</span>(<span class="pl-s rich-diff-level-one" style="box-sizing: border-box; color: rgb(24, 54, 145);"><span class="pl-pds" style="box-sizing: border-box;">"</span>Simple Copy<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
<span class="rich-diff-level-one pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">default</span>:
<span class="pl-c1 rich-diff-level-one" style="box-sizing: border-box; color: rgb(0, 134, 179);">print</span>(<span class="pl-s rich-diff-level-one" style="box-sizing: border-box; color: rgb(24, 54, 145);"><span class="pl-pds" style="box-sizing: border-box;">"</span>Something else<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
}
}</pre></div><div class=""><br class=""></div><div class="">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`.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><blockquote type="cite" class="">On Dec 11, 2015, at 5:43 PM, Brent Royal-Gordon <<a href="mailto:brent@architechies.com" class="">brent@architechies.com</a>> wrote:<br class=""><br class=""><blockquote type="cite" class="">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.<br class=""></blockquote><br class="">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.<br class=""><br class="">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.<br class=""><br class="">-- <br class="">Brent Royal-Gordon<br class="">Architechies<br class=""><br class=""></blockquote><br class=""></div></body></html>