Besides my concern that this is not in scope for Swift 4 phase 1, or for the standard library generally, the function as proposed is unsatisfactory because the behavior is incorrect when lower &gt; upper.<br><br>Either<br><br>(a) the parameters should be labeled (and for consistency with ranges, those labels should be &quot;lowerBound&quot; and &quot;upperBound&quot;) and a precondition should exist before evaluation of the return value, or<br><br>(b) this function should take two arguments, one of these being a Range, or<br><br>(c) better still--this should be a method on numeric types or on Range. I&#39;m not convinced that there is a use case for clamping values of any Comparable type. What does it mean to clamp a String, for example?<br><br>Finally, it could possibly be more performant to compute the value using the comparison operators &lt; and &gt; directly instead of using the functions max and min. It would be nice to have benchmarks on that.<br><br>But I am still not convinced that this function is important enough *and* susceptible enough to an incorrect implementation that the standard library should provide its own.<br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 2, 2016 at 11:56 Nicholas Maccharoli 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 dir="ltr"><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)"><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;">Introduction</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;;font-size:16px;line-height:24px">The aim of this proposal is to introduce <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code> functionality to the swift standard library. The <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code> function would take a value and a upper and lower bounds to <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code> the value to, ensuring that the return value of the function is within the range specified by the upper and lower bounds. Swift-evolution thread: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160829/026748.html" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">Discussion thread topic for that proposal</a></p><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;"><a href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#motivation" style="color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent" target="_blank"></a>Motivation</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;;font-size:16px;line-height:24px">The standard library already includes functionality for <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">min</code> and <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">max</code> but at the time of this writing there is no global function for <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code>. Adding a <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code> function would complement <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">min</code> and <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">max</code> really well and would also eliminate the need for developers to write their own clamp functionality.</p><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;"><a href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#proposed-solution" style="color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent" target="_blank"></a>Proposed solution</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;;font-size:16px;line-height:24px">I propose that a global <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code> function akin to <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">min</code> and <code style="font-family:consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">max</code> be added to the standard library.</p><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;"><a href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#detailed-design" style="color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent" target="_blank"></a>Detailed design</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;;font-size:16px;line-height:24px">The implementation of the global clamp function would be as follows:</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;;font-size:16px;line-height:24px"><pre style="font-family:consolas,&quot;liberation mono&quot;,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;border-radius:3px;word-break:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">public</span> <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">clamp</span>&lt;T <span style="color:rgb(167,29,93)">:</span> <span style="color:rgb(0,134,179)">Comparable</span>&gt;(value: T, <span style="color:rgb(121,93,163)">_</span> <span>lower</span>: T, <span style="color:rgb(121,93,163)">_</span> <span>upper</span>: T) <span style="color:rgb(167,29,93)">-&gt;</span> T {
  <span style="color:rgb(167,29,93)">return</span> <span style="color:rgb(0,134,179)">max</span>(lower, <span style="color:rgb(0,134,179)">min</span>(value, upper))
}</pre></div><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;"><a href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#impact-on-existing-code" style="color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent" target="_blank"></a>Impact on existing code</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;;font-size:16px;line-height:24px">There are no impacts on existing code, this proposal is purely additive.</p><h2 style="margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;"><a href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#alternatives-considered" style="color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent" target="_blank"></a>Alternatives considered</h2><p style="margin-top:0px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,&quot;segoe ui&quot;,roboto,helvetica,arial,sans-serif,&quot;apple color emoji&quot;,&quot;segoe ui emoji&quot;,&quot;segoe ui symbol&quot;;font-size:16px;line-height:24px;margin-bottom:0px">Aside from not making the additions that this proposal wishes to make to the standard library no other alternatives have been considered.</p></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>