<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">+1<div class=""><br class=""></div><div class="">I think `clamp` is a very reasonable function to include in the standard library. The semantics are clear, and it is definitely much easier to read than the alternative.<div class=""><br class=""></div><div class="">I think we should also include an open range variant. Note that this is only defined on types with integer strides since the semantics only make sense when values of the type are discrete.</div><div class=""><br class=""></div><div class=""><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 11.899999618530273px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal; color: rgb(36, 41, 46);" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">extension</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);"><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Strideable</span></span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">where</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Stride</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-e" style="box-sizing: border-box; color: rgb(121, 93, 163);"><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Integer</span></span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">clamped</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">to</span> <span class="pl-smi" style="box-sizing: border-box; color: rgb(51, 51, 51);">range</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Range</span><<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Self</span>>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Self</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">clamped</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">to</span>: range.<span class="pl-smi" style="box-sizing: border-box; color: rgb(51, 51, 51);">lowerBound</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span>(range.<span class="pl-smi" style="box-sizing: border-box; color: rgb(51, 51, 51);">upperBound</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">1</span>))
}
}</pre><div class=""><br class=""></div></div><div class="">Cheers,</div><div class="">Jaden Geller</div><div class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 12, 2017, at 10:16 AM, Nicholas Maccharoli via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">Swift-Evolution.</div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">This is a draft of a proposal adding `clamped(to:)` as an extension on `Comparable`.</div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">For now I have only included a version of the `clamped(to:)` function that accepts `ClosedRange` but if the community decided that it is feasible to add a version that takes half open ranges I would also love to include that as well in this proposal. </div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">That you so much for the feedback so far and I am looking forward <br class=""></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">to the community's feedback on this draft. </div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">- Nick </div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></div><h1 style="box-sizing:border-box;margin:0px 0px 16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"" class="">Add clamp<div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19);display:inline">ed</div>(to:) to the stdlib</h1><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class=""><li style="box-sizing:border-box" class="">Proposal: <a href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" class="">SE-NNNN</a></li><li style="box-sizing:border-box;margin-top:0.25em" class="">Authors: <a href="https://github.com/Nirma" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" class="">Nicholas Maccharoli</a></li><li style="box-sizing:border-box;margin-top:0.25em" class="">Review Manager: TBD</li><li style="box-sizing:border-box;margin-top:0.25em" class="">Status: <span style="box-sizing:border-box;font-weight:600" class="">Awaiting review</span></li></ul><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class=""><em style="box-sizing:border-box" class="">During the review process, add the following fields as needed:</em></p><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class=""><li style="box-sizing:border-box" class="">Decision Notes: <a href="https://lists.swift.org/pipermail/swift-evolution/" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" class="">Rationale</a>, <a href="https://lists.swift.org/pipermail/swift-evolution/" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" class="">Additional Commentary</a></li><li style="box-sizing:border-box;margin-top:0.25em" class="">Bugs: <a href="https://bugs.swift.org/browse/SR-NNNN" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" class="">SR-NNNN</a>, <a href="https://bugs.swift.org/browse/SR-MMMM" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" class="">SR-MMMM</a></li><li style="box-sizing:border-box;margin-top:0.25em" class="">Previous Revision: <a href="https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" class="">1</a></li><li style="box-sizing:border-box;margin-top:0.25em" class="">Previous Proposal: <a href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" class="">SE-XXXX</a></li></ul><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"" class=""><a id="gmail-user-content-introduction" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Introduction</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">This proposal aims to add functionality to the standard library for clamping a value to a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">ClosedRange</code>. The proposed function would allow the user to specify a range to clamp a value to where if the value fell within the range, the value would be returned as is, if the value being clamped exceeded the upper or lower bound in value the value of the boundary the value exceeded would be returned.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">Swift-evolution thread: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" class="">Add a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">clamp</code> function to Algorithm.swift</a></p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"" class=""><a id="gmail-user-content-motivation" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Motivation</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">There have been quite a few times in my professional and personal programming life where I reached for a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">clamped</code>function and was disappointed it was not part of the standard library.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">Having functionality like <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">clamped(to:)</code> added to <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">Comparable</code> as a protocol extension would benefit users of the Swift language who wish to guarantee that a value is kept within bounds.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"" class=""><a id="gmail-user-content-proposed-solution" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Proposed solution</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">The solution proposed is simply that there be a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">clamped(to:)</code> function added to the Swift Standard Library. The function would behave much like its name describes.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">Given a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">clamped(to:)</code> function existed it could be called in the following way, yielding the results in the adjacent comments:</p><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-radius:3px;word-break:normal" class=""><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> foo <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">100</span>
foo.<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamp</span>(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">0</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">50</span>) <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">//</span> 50</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>foo.<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamp</span>(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">200</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">300</span>) <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">//</span> 200</span>
<span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>foo.<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamp</span>(<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">0</span><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">150</span>) <span class="gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="gmail-pl-c" style="box-sizing:border-box">//</span> 100</span></pre></div><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"" class=""><a id="gmail-user-content-detailed-design" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Detailed design</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">The implementation for <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">clamped(to:)</code> as an extension to <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">Comparable</code> accepting a range of type <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">ClosedRange<Self></code>would look like the following:</p><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-radius:3px;word-break:normal" class=""><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">extension</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)"><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Comparable</span></span> {
<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">clamped</span>(<span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">to</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">range</span>: <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">ClosedRange</span><<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Self</span>>) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-></span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Self</span> {
<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">></span> range.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">upperBound</span> {
<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> range.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">upperBound</span>
} <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)"><</span> range.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">lowerBound</span> {
<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> range.<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">lowerBound</span>
} <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> {
<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span>
}
}
}</pre></div><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"" class=""><a id="gmail-user-content-source-compatibility" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Source compatibility</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">This feature is purely additive; it has no effect on source compatibility.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"" class=""><a id="gmail-user-content-effect-on-abi-stability" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Effect on ABI stability</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">This feature is purely additive; it has no effect on ABI stability.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"" class=""><a id="gmail-user-content-effect-on-api-resilience" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Effect on API resilience</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">The proposed function would become part of the API but purely additive.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"" class=""><a id="gmail-user-content-alternatives-considered" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1"></a>Alternatives considered</h2><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><code style="color:rgb(36,41,46);box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">clamped(to:)</code><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class=""> </span><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">could be made a global function like</span><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class=""> </span><code style="color:rgb(36,41,46);box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">min</code><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class=""> </span><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">and</span><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class=""> </span><code style="color:rgb(36,41,46);box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px" class="">max</code><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class=""> </span><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px" class="">but a protocol extension seems like a better choice.</span></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></div></div></body></html>