<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Jan 11, 2018, at 4:21 PM, Paul Cantrell <<a href="mailto:cantrell@pobox.com" class="">cantrell@pobox.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">This raises a question related to Chris’s: what is the utility of having Limb conform to a protocol instead of just providing allValues ad hoc? Does CaseEnumerable / ValueEnumerable serve any purpose other than triggering special behavior in the compiler? Would the protocol ever be used as the type of something in code?</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">My answers, admittedly weak ones, are: (1) conventions are nice and consistency is nice, and (2) you never know how an abstraction might be used, but you do know that people will be angry when it should fit but doesn’t. I can’t come up with a more compelling or specific argument than those.</div></div></blockquote><br class=""></div><div>Here's a place where you might want to use the protocol: Suppose you're writing a table view data source that displays editable controls for a form. You support several different types of controls, one of which is a list of choices for a picker controller.</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space: pre;">        </span>enum Control<Value> {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>case textField</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>case picker(choices: [Value])</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>…</div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>func makeView() -> UIView { … }</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>subscript(valueOf view: UIView) -> Value { get { … } set { … } }</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br class=""></div><div>Presumably you end up writing a schema which looks something like:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>formDataSource = FormDataSource(value: person)</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>formDataSource.fields = [</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>Section(title: nil, fields: [</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>Field(title: "Name", keyPath: \.name, control: .textField),</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>Field(title: "Gender", keyPath: \.gender, control: . picker(choices: Array(Gender.allValues))),</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>…</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>])</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>]</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>tableView.dataSource = formDataSource</div><div><br class=""></div><div>The `Array(Gender.allValues)` here is clutter; it'd be nice if we didn't have to write it explicitly. After all, if you're choosing a value of something you know is an enum, it's sensible to assume that you want to choose from all values if it doesn't specify anything more specific. If `ValueEnumerable` is a formalized protocol, you can do that with a constrained extension:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>extension Control where Value: ValueEnumerable {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>static var picker: Control {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>return .picker(choices: Array(Value.allValues))</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br class=""></div><div>And now you just need to write:</div><div><br class=""></div><div><div><span class="Apple-tab-span" style="white-space: pre;">                        </span>Field(title: "Gender", keyPath: \.gender, control: . picker)</div><div class=""><br class=""></div><div class="">Basically, having `ValueEnumerable` be a formal protocol means we can extend this small automatic behavior into larger automatic behaviors. I can imagine, for instance, building a fuzzer which, given a list of `WritableKeyPath`s whose types are all `ValueEnumerable`, automatically generates random instances of a type and feeds them into a test function. If we get read-write reflection, we might be able to do this automatically and recursively. That'd be pretty cool.</div><div class=""><br class=""></div></div><div class="">
<span class="Apple-style-span" style="border-collapse: separate; font-variant-ligatures: normal; font-variant-east-asian: normal; font-variant-position: normal; line-height: normal; border-spacing: 0px;"><div class=""><div style="font-size: 12px; " class="">-- </div><div style="font-size: 12px; " class="">Brent Royal-Gordon</div><div style="font-size: 12px; " class="">Architechies</div></div></span>
</div>
<br class=""></body></html>