<div dir="ltr">Big +1. I had similar thoughts a while back when I was writing some C++ or Java code that had boolean arguments, and I found myself hating how non-documenting they were and I would contrive two-valued enums to make call sites look better. Having a crisp clean syntax for this would be fantastic and encourage people to write self-documenting APIs.<div><br></div><div>Having argument labels solves some of the problems that come along with boolean arguments, but &quot;fitImage&quot; is a great example where the false case (&quot;not fit?&quot;) doesn&#39;t really convey enough information (or can convey misleading information).</div><div><br><br><div class="gmail_quote"><div dir="ltr">On Tue, May 31, 2016 at 9:17 AM Erica Sadun via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Here&#39;s a function signature from some code from today:<div><br></div><div><table style="border-spacing:0px;border-collapse:collapse;color:rgb(51,51,51);font-family:Helvetica,arial,nimbussansl,liberationsans,freesans,clean,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:13px"><tbody><tr><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap"><span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">scaleAndCropImage</span>(</td></tr><tr><td style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap">    image: UIImage,</td></tr><tr><td style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap">    <span style="color:rgb(121,93,163)">toSize</span> <span>size</span>: CGSize,</td></tr><tr><td style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap">    <b>fitImage: <span style="color:rgb(0,134,179)">Bool</span> <span style="color:rgb(167,29,93)">=</span> <span style="color:rgb(0,134,179)">true</span></b></td></tr><tr><td style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap">    ) <span style="color:rgb(167,29,93)">-&gt;</span> UIImage {</td></tr></tbody></table><div><br></div><div><br></div><div>And here&#39;s what I want the function signature to actually look like:</div></div><div><br></div><div><table style="border-spacing:0px;border-collapse:collapse;color:rgb(51,51,51);font-family:Helvetica,arial,nimbussansl,liberationsans,freesans,clean,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:13px"><tbody><tr><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap"><span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">scaleAndCropImage</span>(</td></tr><tr><td style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap">    image: UIImage,</td></tr><tr><td style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap">    <span style="color:rgb(121,93,163)">toSize</span> <span>size</span>: CGSize,</td></tr><tr><td style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap">    <b>operation: <font color="#0086b3">(.Fit | .Fill)</font> <span style="color:rgb(167,29,93)">=</span> .<font color="#0086b3">Fit</font></b></td></tr><tr><td style="padding:0px 10px;width:50px;min-width:50px;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;line-height:18px;color:rgba(0,0,0,0.298039);text-align:right;white-space:nowrap;vertical-align:top;border-style:solid;border-color:rgb(238,238,238);border-width:0px 1px 0px 0px"></td><td style="padding:0px 10px;vertical-align:top;overflow:visible;font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:12px;word-wrap:normal;white-space:pre-wrap">    ) <span style="color:rgb(167,29,93)">-&gt;</span> UIImage {</td></tr></tbody></table><div><br></div></div><div><br></div><div>where I don&#39;t have to establish a separate enumeration to include ad-hoc enumeration-like semantics for the call. A while back, Yong hee Lee introduced anonymous enumerations (and the possibility of anonymous option flags) but the discussion rather died.</div><div><br></div><div>I&#39;m bringing it up again to see whether there is any general interest in pursuing this further as I think the second example is more readable, appropriate, and Swifty than the first, provides better semantics, and is more self documenting.</div><div><br></div><div>Thanks for your feedback,</div><div><br></div><div>-- Erica</div><div><br></div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div></div></div>