# \[draft\] Add \`clamped(to:)\` to the stdlib

**URL:** <https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442>\
**Category:** Discussion\
**Created:** [March 14, 2017, 2:16am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442 "2017-03-14T02:16:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Maccharoli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicholas_maccharoli/32/6677_2.png) [@Nicholas\_Maccharoli](https://forums.swift.org/u/Nicholas_Maccharoli)\
**Post date:** [March 14, 2017, 2:16am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/1 "2017-03-14T02:16:10Z")

</div>

Swift-Evolution,

After considering the indispensable feedback from the community here is a  
slightly more polished draft.

If anyone is interested the draft can be downloaded here:  
[https://github.com/Nirma/swift-evolution/blob/clamp\_](https://github.com/Nirma/swift-evolution/blob/clamp_)  
function/proposals/NNNN-add-clamp-function.md

Thank you so much,

- Nick

Add clamp(to:) to the stdlib

&nbsp;&nbsp;&nbsp;- Proposal: SE-NNNN  
&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md&gt);  
&nbsp;&nbsp;&nbsp;- Authors: Nicholas Maccharoli \<[https://github.com/Nirma&gt](https://github.com/Nirma&gt);  
&nbsp;&nbsp;&nbsp;- Review Manager: TBD  
&nbsp;&nbsp;&nbsp;- Status: Awaiting review

\*During the review process, add the following fields as needed:\*

&nbsp;&nbsp;&nbsp;- Decision Notes: Rationale  
&nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional  
&nbsp;&nbsp;&nbsp;Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
&nbsp;&nbsp;&nbsp;- Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM  
&nbsp;&nbsp;&nbsp;\<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
&nbsp;&nbsp;&nbsp;- Previous Revision: 1  
&nbsp;&nbsp;&nbsp;\<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
&nbsp;&nbsp;&nbsp;- Previous Proposal: SE-XXXX  
&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/XXXX-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md&gt);

\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#introduction&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction&gt);  
Introduction

This proposal aims to add functionality to the standard library for  
clamping a value to a ClosedRange. 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.

Swift-evolution thread: Add a clamp function to Algorithm.swift  
\<[The swift-evolution The Week Of Monday 6 March 2017 Archive by thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674&gt);  
\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#motivation&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation&gt);  
Motivation

There have been quite a few times in my professional and personal  
programming life where I reached for a clampedfunction and was disappointed  
it was not part of the standard library.

Having functionality like clamped(to:) added to Comparable as a protocol  
extension would benefit users of the Swift language who wish to guarantee  
that a value is kept within bounds.  
\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed)  
solution

The solution proposed is simply that there be a clamped(to:) function added  
to the Swift Standard Library. The function would behave much like its name  
describes.

Given a clamped(to:) function existed it could be called in the following  
way, yielding the results in the adjacent comments:

let foo = 100  
// Closed range variant  
foo.clamped(to: 0...50) // 50foo.clamped(to: 200...300) //  
200foo.clamped(to: 0...150) // 100// Half-Open range variant  
foo.clamped(to: 0..\<50) // 49foo.clamped(to: 200..\<300) //  
200foo.clamped(to: 0..\<150) // 100

\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed)  
design

The implementation of clamped(to:) that is being proposed is composed of  
two protocol extensions; one protocol extension on Comparable and another  
on Strideable.

The implementation for clamped(to:) as an extension to Comparable accepting  
a range of type ClosedRange\<Self\>would look like the following:

extension Comparable {  
&nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: ClosedRange\<Self\>) -\> Self {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self \> range.upperBound {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.upperBound  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if self \< range.lowerBound {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.lowerBound  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

The implementation of clamped(to:) as an extension on Strideable would be  
confined to cases where the stride is of type Integer. The implementation  
would be as follows:

extension Strideable where Stride: Integer {  
&nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source)  
compatibility

This feature is purely additive; it has no effect on source compatibility.  
\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect)  
on ABI stability

This feature is purely additive; it has no effect on ABI stability.  
\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect)  
on API resilience

The proposed function would become part of the API but purely additive.  
\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives)  
considered

Aside from doing nothing, no other alternatives were considered.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Maccharoli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicholas_maccharoli/32/6677_2.png) [@Nicholas\_Maccharoli](https://forums.swift.org/u/Nicholas_Maccharoli)\
**Post date:** [March 15, 2017, 2:07am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/2 "2017-03-15T02:07:54Z")

</div>

I am starting to worry that I left something out.

> **···**
>
> On Tue, Mar 14, 2017 at 11:16 AM, Nicholas Maccharoli \<nmaccharoli@gmail.com \> wrote:
> 
> > Swift-Evolution,
> > 
> > After considering the indispensable feedback from the community here is a  
> > slightly more polished draft.
> > 
> > If anyone is interested the draft can be downloaded here:  
> > [https://github.com/Nirma/swift-evolution/blob/clamp\_function](https://github.com/Nirma/swift-evolution/blob/clamp_function)  
> > /proposals/NNNN-add-clamp-function.md
> > 
> > Thank you so much,
> > 
> > - Nick
> > 
> > Add clamp(to:) to the stdlib
> > 
> > &nbsp;&nbsp;&nbsp;- Proposal: SE-NNNN  
> > &nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md&gt);  
> > &nbsp;&nbsp;&nbsp;- Authors: Nicholas Maccharoli \<[https://github.com/Nirma&gt](https://github.com/Nirma&gt);  
> > &nbsp;&nbsp;&nbsp;- Review Manager: TBD  
> > &nbsp;&nbsp;&nbsp;- Status: Awaiting review
> > 
> > \*During the review process, add the following fields as needed:\*
> > 
> > &nbsp;&nbsp;&nbsp;- Decision Notes: Rationale  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional  
> > &nbsp;&nbsp;&nbsp;Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > &nbsp;&nbsp;&nbsp;- Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM  
> > &nbsp;&nbsp;&nbsp;\<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> > &nbsp;&nbsp;&nbsp;- Previous Revision: 1  
> > &nbsp;&nbsp;&nbsp;\<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> > &nbsp;&nbsp;&nbsp;- Previous Proposal: SE-XXXX  
> > &nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/XXXX-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md&gt);
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#introduction&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction&gt);  
> > Introduction
> > 
> > This proposal aims to add functionality to the standard library for  
> > clamping a value to a ClosedRange. 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.
> > 
> > Swift-evolution thread: Add a clamp function to Algorithm.swift  
> > \<[The swift-evolution The Week Of Monday 6 March 2017 Archive by thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674&gt);
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#motivation&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation&gt);  
> > Motivation
> > 
> > There have been quite a few times in my professional and personal  
> > programming life where I reached for a clampedfunction and was  
> > disappointed it was not part of the standard library.
> > 
> > Having functionality like clamped(to:) added to Comparable as a protocol  
> > extension would benefit users of the Swift language who wish to guarantee  
> > that a value is kept within bounds.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed)  
> > solution
> > 
> > The solution proposed is simply that there be a clamped(to:) function  
> > added to the Swift Standard Library. The function would behave much like  
> > its name describes.
> > 
> > Given a clamped(to:) function existed it could be called in the following  
> > way, yielding the results in the adjacent comments:
> > 
> > let foo = 100  
> > // Closed range variant  
> > foo.clamped(to: 0...50) // 50foo.clamped(to: 200...300) // 200foo.clamped(to: 0...150) // 100// Half-Open range variant  
> > foo.clamped(to: 0..\<50) // 49foo.clamped(to: 200..\<300) // 200foo.clamped(to: 0..\<150) // 100
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed)  
> > design
> > 
> > The implementation of clamped(to:) that is being proposed is composed of  
> > two protocol extensions; one protocol extension on Comparable and another  
> > on Strideable.
> > 
> > The implementation for clamped(to:) as an extension to Comparable accepting  
> > a range of type ClosedRange\<Self\>would look like the following:
> > 
> > extension Comparable {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: ClosedRange\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self \> range.upperBound {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.upperBound  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if self \< range.lowerBound {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.lowerBound  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > The implementation of clamped(to:) as an extension on Strideable would be  
> > confined to cases where the stride is of type Integer. The implementation  
> > would be as follows:
> > 
> > extension Strideable where Stride: Integer {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source)  
> > compatibility
> > 
> > This feature is purely additive; it has no effect on source compatibility.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect)  
> > on ABI stability
> > 
> > This feature is purely additive; it has no effect on ABI stability.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect)  
> > on API resilience
> > 
> > The proposed function would become part of the API but purely additive.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives)  
> > considered
> > 
> > Aside from doing nothing, no other alternatives were considered.

---

<div class="post-metadata">

**Author:** ![nnnnnnnn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nnnnnnnn/32/264_2.png) [@nnnnnnnn](https://forums.swift.org/u/nnnnnnnn)\
**Post date:** [March 15, 2017, 7:40pm UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/3 "2017-03-15T19:40:14Z")

</div>

Hi Nick,

I think this might have come up in an earlier thread, but clamped(to:) with a half-open range argument brings up the question of how to clamp to an empty range. As proposed, the implementation of this method would result in an error when passed an empty range:

&nbsp;&nbsp;&nbsp;&nbsp;foo.clamped(to: 0..\<0)  
&nbsp;&nbsp;&nbsp;&nbsp;// fatal error: Can't form Range with upperBound \< lowerBound

Is that the desired behavior? Providing documentation for both new methods would help make it clear what the expected functionality is.

Thanks!  
Nate

> **···**
>
> > On Mar 13, 2017, at 9:16 PM, Nicholas Maccharoli via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Swift-Evolution,
> > 
> > After considering the indispensable feedback from the community here is a slightly more polished draft.
> > 
> > If anyone is interested the draft can be downloaded here:  
> > [https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md)
> > 
> > Thank you so much,
> > 
> > - Nick
> > 
> > Add clamp(to:) to the stdlib
> > 
> > Proposal: SE-NNNN \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md&gt);  
> > Authors: Nicholas Maccharoli \<[https://github.com/Nirma&gt](https://github.com/Nirma&gt);  
> > Review Manager: TBD  
> > Status: Awaiting review  
> > During the review process, add the following fields as needed:
> > 
> > Decision Notes: Rationale \<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> > Previous Revision: 1 \<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> > Previous Proposal: SE-XXXX \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/XXXX-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md&gt);  
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#introduction&gt;Introduction](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction&gt;Introduction)
> > 
> > This proposal aims to add functionality to the standard library for clamping a value to a ClosedRange. 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.
> > 
> > Swift-evolution thread: Add a clamp function to Algorithm.swift \<[The swift-evolution The Week Of Monday 6 March 2017 Archive by thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674&gt);  
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#motivation&gt;Motivation](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation&gt;Motivation)
> > 
> > There have been quite a few times in my professional and personal programming life where I reached for a clampedfunction and was disappointed it was not part of the standard library.
> > 
> > Having functionality like clamped(to:) added to Comparable as a protocol extension would benefit users of the Swift language who wish to guarantee that a value is kept within bounds.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed) solution
> > 
> > The solution proposed is simply that there be a clamped(to:) function added to the Swift Standard Library. The function would behave much like its name describes.
> > 
> > Given a clamped(to:) function existed it could be called in the following way, yielding the results in the adjacent comments:
> > 
> > let foo = 100
> > 
> > // Closed range variant
> > 
> > foo.clamped(to: 0...50) // 50  
> > foo.clamped(to: 200...300) // 200  
> > foo.clamped(to: 0...150) // 100
> > 
> > // Half-Open range variant
> > 
> > foo.clamped(to: 0..\<50) // 49  
> > foo.clamped(to: 200..\<300) // 200  
> > foo.clamped(to: 0..\<150) // 100  
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed) design
> > 
> > The implementation of clamped(to:) that is being proposed is composed of two protocol extensions; one protocol extension on Comparable and another on Strideable.
> > 
> > The implementation for clamped(to:) as an extension to Comparable accepting a range of type ClosedRange\<Self\>would look like the following:
> > 
> > extension Comparable {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: ClosedRange\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self \> range.upperBound {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.upperBound  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if self \< range.lowerBound {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.lowerBound  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }  
> > The implementation of clamped(to:) as an extension on Strideable would be confined to cases where the stride is of type Integer. The implementation would be as follows:
> > 
> > extension Strideable where Stride: Integer {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }  
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source) compatibility
> > 
> > This feature is purely additive; it has no effect on source compatibility.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect) on ABI stability
> > 
> > This feature is purely additive; it has no effect on ABI stability.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect) on API resilience
> > 
> > The proposed function would become part of the API but purely additive.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives) considered
> > 
> > Aside from doing nothing, no other alternatives were considered.  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Neil\_Cowburn](https://avatars.discourse-cdn.com/v4/letter/n/c4cdca/32.png) [@Neil\_Cowburn](https://forums.swift.org/u/Neil_Cowburn)\
**Post date:** [March 15, 2017, 10:22am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/4 "2017-03-15T10:22:15Z")

</div>

A suggestion, if I may: change the verb `clamped` to `bound`. The verb `clamp` means to immobilise or fix in place. That seems counterintuitive given the sole function argument is a range.

Compare the interpretation of clamped vs bound:

foo.clamped(to: 0...100) --\> “The integer, foo, is clamped to a range between 0 and 100”

foo.bound(to: 0...100) --\> “The integer, foo, is bound to a range between 0 and 100”

The latter, to me, reads more naturally than the former.

-- Neil

> **···**
>
> On 15/03/2017, 02:07, "Nicholas Maccharoli via swift-evolution" \<swift-evolution-bounces@swift.org on behalf of swift-evolution@swift.org\> wrote:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;I am starting to worry that I left something out.  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;On Tue, Mar 14, 2017 at 11:16 AM, Nicholas Maccharoli \<nmaccharoli@gmail.com\> wrote:  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;Swift-Evolution,  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;After considering the indispensable feedback from the community here is a slightly more polished draft.  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;If anyone is interested the draft can be downloaded here:  
> &nbsp;&nbsp;&nbsp;&nbsp;[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md)  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;Thank you so much,  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;- Nick  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;Add clamp(to:) to the stdlib  
> &nbsp;&nbsp;&nbsp;&nbsp;\* Proposal: SE-NNNN \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md&gt);  
> &nbsp;&nbsp;&nbsp;&nbsp;\* Authors: Nicholas Maccharoli \<[https://github.com/Nirma&gt](https://github.com/Nirma&gt);  
> &nbsp;&nbsp;&nbsp;&nbsp;\* Review Manager: TBD  
> &nbsp;&nbsp;&nbsp;&nbsp;\* Status: Awaiting review  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;During the review process, add the following fields as needed:  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;\* Decision Notes: Rationale \<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> &nbsp;&nbsp;&nbsp;&nbsp;\* Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> &nbsp;&nbsp;&nbsp;&nbsp;\* Previous Revision: 1 \<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> &nbsp;&nbsp;&nbsp;&nbsp;\* Previous Proposal: SE-XXXX \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/XXXX-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md&gt);  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#introduction&gt;IntroductionThis](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction&gt;IntroductionThis) proposal aims to add functionality to the standard library for clamping a value to a ClosedRange. 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.  
> &nbsp;&nbsp;&nbsp;&nbsp;Swift-evolution thread: Add a clamp function to Algorithm.swift \<[The swift-evolution The Week Of Monday 6 March 2017 Archive by thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674&gt);  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#motivation&gt;MotivationThere](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation&gt;MotivationThere) have been quite a few times in my professional and personal programming life where I reached for a clampedfunction and was disappointed it was not part of the standard library.  
> &nbsp;&nbsp;&nbsp;&nbsp;Having functionality like clamped(to:) added to Comparable as a protocol extension would benefit users of the Swift language who wish to guarantee that a value is kept within bounds.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed) solutionThe solution proposed is simply that there be a clamped(to:) function added to the Swift Standard Library. The function would behave much like its name describes.  
> &nbsp;&nbsp;&nbsp;&nbsp;Given a clamped(to:) function existed it could be called in the following way, yielding the results in the adjacent comments:  
> &nbsp;&nbsp;&nbsp;&nbsp;let foo = 100// Closed range variant  
> &nbsp;&nbsp;&nbsp;&nbsp;foo.clamped(to: 0...50) // 50foo.clamped(to: 200...300) // 200foo.clamped(to: 0...150) // 100// Half-Open range variant  
> &nbsp;&nbsp;&nbsp;&nbsp;foo.clamped(to: 0..\<50) // 49foo.clamped(to: 200..\<300) // 200foo.clamped(to: 0..\<150) // 100  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed) designThe implementation of clamped(to:) that is being proposed is composed of two protocol extensions; one protocol extension on Comparable and another on Strideable.  
> &nbsp;&nbsp;&nbsp;&nbsp;The implementation for clamped(to:) as an extension to Comparable accepting a range of type ClosedRange\<Self\>would look like the following:  
> &nbsp;&nbsp;&nbsp;&nbsp;extension Comparable {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: ClosedRange\<Self\>) -\> Self {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self \> range.upperBound {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.upperBound  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if self \< range.lowerBound {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.lowerBound  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;The implementation of clamped(to:) as an extension on Strideable would be confined to cases where the stride is of type Integer. The implementation would be as follows:  
> &nbsp;&nbsp;&nbsp;&nbsp;extension Strideable where Stride: Integer {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source) compatibilityThis feature is purely additive; it has no effect on source compatibility.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect) on ABI stabilityThis feature is purely additive; it has no effect on ABI stability.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect) on API resilienceThe proposed function would become part of the API but purely additive.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives) consideredAside from doing nothing, no other alternatives were considered.  
> &nbsp;&nbsp;&nbsp;&nbsp;  
> &nbsp;&nbsp;&nbsp;&nbsp;\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> &nbsp;&nbsp;&nbsp;&nbsp;swift-evolution mailing list  
> &nbsp;&nbsp;&nbsp;&nbsp;swift-evolution@swift.org  
> &nbsp;&nbsp;&nbsp;&nbsp;[https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![David\_Ronnqvist](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@David\_Ronnqvist](https://forums.swift.org/u/David_Ronnqvist)\
**Post date:** [March 15, 2017, 10:05am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/5 "2017-03-15T10:05:00Z")

</div>

It might be worth mentioning (for background) that the various Range types already have a `clamped(to:)`. The proposed solution is consistent with that (as opposed to for example a free function).  
I’m not sure if a free function should be considered an alternative. It’s probably fine not listing any alternatives.  
The motivation section could benefit from one or two examples of what/when clamping is used. That could help someone who haven’t used it to judge it’s usefulness as part of the standard library.

- David

> **···**
>
> > On 15 Mar 2017, at 03:07, Nicholas Maccharoli via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > I am starting to worry that I left something out.
> > 
> > On Tue, Mar 14, 2017 at 11:16 AM, Nicholas Maccharoli \<nmaccharoli@gmail.com \<mailto:nmaccharoli@gmail.com\>\> wrote:  
> > Swift-Evolution,
> > 
> > After considering the indispensable feedback from the community here is a slightly more polished draft.
> > 
> > If anyone is interested the draft can be downloaded here:  
> > [https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md)
> > 
> > Thank you so much,
> > 
> > - Nick
> > 
> > Add clamp(to:) to the stdlib
> > 
> > Proposal: SE-NNNN \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md&gt);  
> > Authors: Nicholas Maccharoli \<[https://github.com/Nirma&gt](https://github.com/Nirma&gt);  
> > Review Manager: TBD  
> > Status: Awaiting review  
> > During the review process, add the following fields as needed:
> > 
> > Decision Notes: Rationale \<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> > Previous Revision: 1 \<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> > Previous Proposal: SE-XXXX \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/XXXX-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md&gt);  
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#introduction&gt;Introduction](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction&gt;Introduction)
> > 
> > This proposal aims to add functionality to the standard library for clamping a value to a ClosedRange. 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.
> > 
> > Swift-evolution thread: Add a clamp function to Algorithm.swift \<[The swift-evolution The Week Of Monday 6 March 2017 Archive by thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674&gt);  
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#motivation&gt;Motivation](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation&gt;Motivation)
> > 
> > There have been quite a few times in my professional and personal programming life where I reached for a clampedfunction and was disappointed it was not part of the standard library.
> > 
> > Having functionality like clamped(to:) added to Comparable as a protocol extension would benefit users of the Swift language who wish to guarantee that a value is kept within bounds.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed) solution
> > 
> > The solution proposed is simply that there be a clamped(to:) function added to the Swift Standard Library. The function would behave much like its name describes.
> > 
> > Given a clamped(to:) function existed it could be called in the following way, yielding the results in the adjacent comments:
> > 
> > let foo = 100
> > 
> > // Closed range variant
> > 
> > foo.clamped(to: 0...50) // 50  
> > foo.clamped(to: 200...300) // 200  
> > foo.clamped(to: 0...150) // 100
> > 
> > // Half-Open range variant
> > 
> > foo.clamped(to: 0..\<50) // 49  
> > foo.clamped(to: 200..\<300) // 200  
> > foo.clamped(to: 0..\<150) // 100  
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed) design
> > 
> > The implementation of clamped(to:) that is being proposed is composed of two protocol extensions; one protocol extension on Comparable and another on Strideable.
> > 
> > The implementation for clamped(to:) as an extension to Comparable accepting a range of type ClosedRange\<Self\>would look like the following:
> > 
> > extension Comparable {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: ClosedRange\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self \> range.upperBound {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.upperBound  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if self \< range.lowerBound {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.lowerBound  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }  
> > The implementation of clamped(to:) as an extension on Strideable would be confined to cases where the stride is of type Integer. The implementation would be as follows:
> > 
> > extension Strideable where Stride: Integer {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }  
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source) compatibility
> > 
> > This feature is purely additive; it has no effect on source compatibility.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect) on ABI stability
> > 
> > This feature is purely additive; it has no effect on ABI stability.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect) on API resilience
> > 
> > The proposed function would become part of the API but purely additive.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives) considered
> > 
> > Aside from doing nothing, no other alternatives were considered.
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![BigZaphod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bigzaphod/32/9561_2.png) [@BigZaphod](https://forums.swift.org/u/BigZaphod)\
**Post date:** [March 15, 2017, 4:33pm UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/6 "2017-03-15T16:33:53Z")

</div>

Clamping in this context is a term of art.

l8r  
Sean

> **···**
>
> > On Mar 15, 2017, at 5:22 AM, Neil Cowburn via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > A suggestion, if I may: change the verb `clamped` to `bound`. The verb `clamp` means to immobilise or fix in place. That seems counterintuitive given the sole function argument is a range.
> > 
> > Compare the interpretation of clamped vs bound:
> > 
> > foo.clamped(to: 0...100) --\> “The integer, foo, is clamped to a range between 0 and 100”
> > 
> > foo.bound(to: 0...100) --\> “The integer, foo, is bound to a range between 0 and 100”
> > 
> > The latter, to me, reads more naturally than the former.
> > 
> > -- Neil
> > 
> > On 15/03/2017, 02:07, "Nicholas Maccharoli via swift-evolution" \<swift-evolution-bounces@swift.org on behalf of swift-evolution@swift.org\> wrote:
> > 
> > &nbsp;&nbsp;&nbsp;I am starting to worry that I left something out.
> > 
> > &nbsp;&nbsp;&nbsp;On Tue, Mar 14, 2017 at 11:16 AM, Nicholas Maccharoli \<nmaccharoli@gmail.com\> wrote:
> > 
> > &nbsp;&nbsp;&nbsp;Swift-Evolution,
> > 
> > &nbsp;&nbsp;&nbsp;After considering the indispensable feedback from the community here is a slightly more polished draft.
> > 
> > &nbsp;&nbsp;&nbsp;If anyone is interested the draft can be downloaded here:  
> > &nbsp;&nbsp;&nbsp;[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md)
> > 
> > &nbsp;&nbsp;&nbsp;Thank you so much,
> > 
> > &nbsp;&nbsp;&nbsp;- Nick
> > 
> > &nbsp;&nbsp;&nbsp;Add clamp(to:) to the stdlib  
> > &nbsp;&nbsp;&nbsp;\* Proposal: SE-NNNN \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md&gt);  
> > &nbsp;&nbsp;&nbsp;\* Authors: Nicholas Maccharoli \<[https://github.com/Nirma&gt](https://github.com/Nirma&gt);  
> > &nbsp;&nbsp;&nbsp;\* Review Manager: TBD  
> > &nbsp;&nbsp;&nbsp;\* Status: Awaiting review
> > 
> > &nbsp;&nbsp;&nbsp;During the review process, add the following fields as needed:
> > 
> > &nbsp;&nbsp;&nbsp;\* Decision Notes: Rationale \<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > &nbsp;&nbsp;&nbsp;\* Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> > &nbsp;&nbsp;&nbsp;\* Previous Revision: 1 \<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> > &nbsp;&nbsp;&nbsp;\* Previous Proposal: SE-XXXX \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/XXXX-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md&gt);
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#introduction&gt;IntroductionThis](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction&gt;IntroductionThis) proposal aims to add functionality to the standard library for clamping a value to a ClosedRange. 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.  
> > &nbsp;&nbsp;&nbsp;Swift-evolution thread: Add a clamp function to Algorithm.swift \<[The swift-evolution The Week Of Monday 6 March 2017 Archive by thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674&gt);  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#motivation&gt;MotivationThere](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation&gt;MotivationThere) have been quite a few times in my professional and personal programming life where I reached for a clampedfunction and was disappointed it was not part of the standard library.  
> > &nbsp;&nbsp;&nbsp;Having functionality like clamped(to:) added to Comparable as a protocol extension would benefit users of the Swift language who wish to guarantee that a value is kept within bounds.  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed) solutionThe solution proposed is simply that there be a clamped(to:) function added to the Swift Standard Library. The function would behave much like its name describes.  
> > &nbsp;&nbsp;&nbsp;Given a clamped(to:) function existed it could be called in the following way, yielding the results in the adjacent comments:  
> > &nbsp;&nbsp;&nbsp;let foo = 100// Closed range variant  
> > &nbsp;&nbsp;&nbsp;foo.clamped(to: 0...50) // 50foo.clamped(to: 200...300) // 200foo.clamped(to: 0...150) // 100// Half-Open range variant  
> > &nbsp;&nbsp;&nbsp;foo.clamped(to: 0..\<50) // 49foo.clamped(to: 200..\<300) // 200foo.clamped(to: 0..\<150) // 100  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed) designThe implementation of clamped(to:) that is being proposed is composed of two protocol extensions; one protocol extension on Comparable and another on Strideable.  
> > &nbsp;&nbsp;&nbsp;The implementation for clamped(to:) as an extension to Comparable accepting a range of type ClosedRange\<Self\>would look like the following:  
> > &nbsp;&nbsp;&nbsp;extension Comparable {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: ClosedRange\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self \> range.upperBound {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.upperBound  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if self \< range.lowerBound {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.lowerBound  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;The implementation of clamped(to:) as an extension on Strideable would be confined to cases where the stride is of type Integer. The implementation would be as follows:  
> > &nbsp;&nbsp;&nbsp;extension Strideable where Stride: Integer {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source) compatibilityThis feature is purely additive; it has no effect on source compatibility.  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect) on ABI stabilityThis feature is purely additive; it has no effect on ABI stability.  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect) on API resilienceThe proposed function would become part of the API but purely additive.  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives) consideredAside from doing nothing, no other alternatives were considered.
> > 
> > &nbsp;&nbsp;&nbsp;\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > &nbsp;&nbsp;&nbsp;swift-evolution mailing list  
> > &nbsp;&nbsp;&nbsp;swift-evolution@swift.org  
> > &nbsp;&nbsp;&nbsp;[https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![jadengeller](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jadengeller/32/11544_2.png) [@jadengeller](https://forums.swift.org/u/jadengeller)\
**Post date:** [March 16, 2017, 1:19am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/7 "2017-03-16T01:19:00Z")

</div>

> Hi Nick,
> 
> I think this might have come up in an earlier thread, but clamped(to:) with a half-open range argument brings up the question of how to clamp to an empty range. As proposed, the implementation of this method would result in an error when passed an empty range:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;foo.clamped(to: 0..\<0)  
> &nbsp;&nbsp;&nbsp;&nbsp;// fatal error: Can't form Range with upperBound \< lowerBound

Ahh, good catch! The implementation ought to add something like:

guard !range.isEmpty else { fatalError("Cannot clamp to an empty range") }

I don’t think that this is problematic. Ideally we could express in the type-system that the range must not be empty, but it is definitely not worthwhile creating a `NonemptyRange` type.

> **···**
>
> > On Mar 15, 2017, at 12:40 PM, Nate Cook via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> > Is that the desired behavior? Providing documentation for both new methods would help make it clear what the expected functionality is.
> > 
> > Thanks!  
> > Nate
> > 
> > > On Mar 13, 2017, at 9:16 PM, Nicholas Maccharoli via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > 
> > > Swift-Evolution,
> > > 
> > > After considering the indispensable feedback from the community here is a slightly more polished draft.
> > > 
> > > If anyone is interested the draft can be downloaded here:  
> > > [https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md)
> > > 
> > > Thank you so much,
> > > 
> > > - Nick
> > > 
> > > Add clamp(to:) to the stdlib
> > > 
> > > Proposal: SE-NNNN \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md&gt);  
> > > Authors: Nicholas Maccharoli \<[https://github.com/Nirma&gt](https://github.com/Nirma&gt);  
> > > Review Manager: TBD  
> > > Status: Awaiting review  
> > > During the review process, add the following fields as needed:
> > > 
> > > Decision Notes: Rationale \<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > > Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> > > Previous Revision: 1 \<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> > > Previous Proposal: SE-XXXX \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/XXXX-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md&gt);  
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#introduction&gt;Introduction](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction&gt;Introduction)
> > > 
> > > This proposal aims to add functionality to the standard library for clamping a value to a ClosedRange. 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.
> > > 
> > > Swift-evolution thread: Add a clamp function to Algorithm.swift \<[The swift-evolution The Week Of Monday 6 March 2017 Archive by thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674&gt);  
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#motivation&gt;Motivation](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation&gt;Motivation)
> > > 
> > > There have been quite a few times in my professional and personal programming life where I reached for a clampedfunction and was disappointed it was not part of the standard library.
> > > 
> > > Having functionality like clamped(to:) added to Comparable as a protocol extension would benefit users of the Swift language who wish to guarantee that a value is kept within bounds.
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed) solution
> > > 
> > > The solution proposed is simply that there be a clamped(to:) function added to the Swift Standard Library. The function would behave much like its name describes.
> > > 
> > > Given a clamped(to:) function existed it could be called in the following way, yielding the results in the adjacent comments:
> > > 
> > > let foo = 100
> > > 
> > > // Closed range variant
> > > 
> > > foo.clamped(to: 0...50) // 50  
> > > foo.clamped(to: 200...300) // 200  
> > > foo.clamped(to: 0...150) // 100
> > > 
> > > // Half-Open range variant
> > > 
> > > foo.clamped(to: 0..\<50) // 49  
> > > foo.clamped(to: 200..\<300) // 200  
> > > foo.clamped(to: 0..\<150) // 100  
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed) design
> > > 
> > > The implementation of clamped(to:) that is being proposed is composed of two protocol extensions; one protocol extension on Comparable and another on Strideable.
> > > 
> > > The implementation for clamped(to:) as an extension to Comparable accepting a range of type ClosedRange\<Self\>would look like the following:
> > > 
> > > extension Comparable {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: ClosedRange\<Self\>) -\> Self {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self \> range.upperBound {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.upperBound  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if self \< range.lowerBound {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.lowerBound  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > }  
> > > The implementation of clamped(to:) as an extension on Strideable would be confined to cases where the stride is of type Integer. The implementation would be as follows:
> > > 
> > > extension Strideable where Stride: Integer {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > }  
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source) compatibility
> > > 
> > > This feature is purely additive; it has no effect on source compatibility.
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect) on ABI stability
> > > 
> > > This feature is purely additive; it has no effect on ABI stability.
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect) on API resilience
> > > 
> > > The proposed function would become part of the API but purely additive.
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives) considered
> > > 
> > > Aside from doing nothing, no other alternatives were considered.  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Haravikk3](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Haravikk3](https://forums.swift.org/u/Haravikk3)\
**Post date:** [March 15, 2017, 10:15pm UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/8 "2017-03-15T22:15:13Z")

</div>

I'd say that's the correct behaviour; it makes no sense to call a clamp method without anything to actually clamp to; an empty range contains nothing, so not only can't include the value you're testing against, but also has no bounds to clamp to instead.

> **···**
>
> > On 15 Mar 2017, at 19:40, Nate Cook via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Hi Nick,
> > 
> > I think this might have come up in an earlier thread, but clamped(to:) with a half-open range argument brings up the question of how to clamp to an empty range. As proposed, the implementation of this method would result in an error when passed an empty range:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.clamped(to: 0..\<0)  
> > &nbsp;&nbsp;&nbsp;&nbsp;// fatal error: Can't form Range with upperBound \< lowerBound
> > 
> > Is that the desired behavior? Providing documentation for both new methods would help make it clear what the expected functionality is.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Maccharoli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicholas_maccharoli/32/6677_2.png) [@Nicholas\_Maccharoli](https://forums.swift.org/u/Nicholas_Maccharoli)\
**Post date:** [March 16, 2017, 2:33am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/9 "2017-03-16T02:33:49Z")

</div>

Right, there were a few things missing!  
Thanks so much for pointing them out everyone.

Dave - Great idea! I have updated the motivation section section as you  
suggested!

Neil - Yes I also think the wording could be a bit better but since the  
word `clamped` is already being used  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I thought I would keep it consistent.

Sean - Looks as if its a term of art to me as well.

Nate,

Good catch! Yes I also thing clamping on an empty range should be a fatal  
error as well.  
An empty range is impossible to create with `ClosedRange` so I left the  
implementation  
of that alone, but it is possible with `Range` so I updated the extension  
on `Strideable` like so:

extension Strideable where Stride: Integer {  
&nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if range.isEmpty { fatalError("Can't form Range with  
upperBound \< lowerBound") }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

Jaden,

Yeah I think a simple `if` check would work as well.

> **···**
>
> On Thu, Mar 16, 2017 at 10:19 AM, Jaden Geller \<jaden.geller@gmail.com\> wrote:
> 
> > On Mar 15, 2017, at 12:40 PM, Nate Cook via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > Hi Nick,
> > 
> > I think this might have come up in an earlier thread, but clamped(to:)  
> > with a half-open range argument brings up the question of how to clamp to  
> > an empty range. As proposed, the implementation of this method would result  
> > in an error when passed an empty range:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;foo.clamped(to: 0..\<0)  
> > &nbsp;&nbsp;&nbsp;&nbsp;// fatal error: Can't form Range with upperBound \< lowerBound
> > 
> > Ahh, good catch! The implementation ought to add something like:
> > 
> > guard !range.isEmpty else { fatalError("Cannot clamp to an empty range") }
> > 
> > I don’t think that this is problematic. Ideally we could express in the  
> > type-system that the range must not be empty, but it is definitely not  
> > worthwhile creating a `NonemptyRange` type.
> > 
> > Is that the desired behavior? Providing documentation for both new methods  
> > would help make it clear what the expected functionality is.
> > 
> > Thanks!  
> > Nate
> > 
> > On Mar 13, 2017, at 9:16 PM, Nicholas Maccharoli via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > Swift-Evolution,
> > 
> > After considering the indispensable feedback from the community here is a  
> > slightly more polished draft.
> > 
> > If anyone is interested the draft can be downloaded here:  
> > [https://github.com/Nirma/swift-evolution/blob/clamp\_function](https://github.com/Nirma/swift-evolution/blob/clamp_function)  
> > /proposals/NNNN-add-clamp-function.md
> > 
> > Thank you so much,
> > 
> > - Nick
> > 
> > Add clamp(to:) to the stdlib
> > 
> > &nbsp;&nbsp;&nbsp;- Proposal: SE-NNNN  
> > &nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md&gt);  
> > &nbsp;&nbsp;&nbsp;- Authors: Nicholas Maccharoli \<[https://github.com/Nirma&gt](https://github.com/Nirma&gt);  
> > &nbsp;&nbsp;&nbsp;- Review Manager: TBD  
> > &nbsp;&nbsp;&nbsp;- Status: Awaiting review
> > 
> > \*During the review process, add the following fields as needed:\*
> > 
> > &nbsp;&nbsp;&nbsp;- Decision Notes: Rationale  
> > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional  
> > &nbsp;&nbsp;&nbsp;Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > &nbsp;&nbsp;&nbsp;- Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM  
> > &nbsp;&nbsp;&nbsp;\<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> > &nbsp;&nbsp;&nbsp;- Previous Revision: 1  
> > &nbsp;&nbsp;&nbsp;\<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> > &nbsp;&nbsp;&nbsp;- Previous Proposal: SE-XXXX  
> > &nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/XXXX-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md&gt);
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#introduction&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction&gt);  
> > Introduction
> > 
> > This proposal aims to add functionality to the standard library for  
> > clamping a value to a ClosedRange. 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.
> > 
> > Swift-evolution thread: Add a clamp function to Algorithm.swift  
> > \<[The swift-evolution The Week Of Monday 6 March 2017 Archive by thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674&gt);
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#motivation&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation&gt);  
> > Motivation
> > 
> > There have been quite a few times in my professional and personal  
> > programming life where I reached for a clampedfunction and was  
> > disappointed it was not part of the standard library.
> > 
> > Having functionality like clamped(to:) added to Comparable as a protocol  
> > extension would benefit users of the Swift language who wish to guarantee  
> > that a value is kept within bounds.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed)  
> > solution
> > 
> > The solution proposed is simply that there be a clamped(to:) function  
> > added to the Swift Standard Library. The function would behave much like  
> > its name describes.
> > 
> > Given a clamped(to:) function existed it could be called in the following  
> > way, yielding the results in the adjacent comments:
> > 
> > let foo = 100  
> > // Closed range variant  
> > foo.clamped(to: 0...50) // 50foo.clamped(to: 200...300) // 200foo.clamped(to: 0...150) // 100// Half-Open range variant  
> > foo.clamped(to: 0..\<50) // 49foo.clamped(to: 200..\<300) // 200foo.clamped(to: 0..\<150) // 100
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed)  
> > design
> > 
> > The implementation of clamped(to:) that is being proposed is composed of  
> > two protocol extensions; one protocol extension on Comparable and another  
> > on Strideable.
> > 
> > The implementation for clamped(to:) as an extension to Comparable accepting  
> > a range of type ClosedRange\<Self\>would look like the following:
> > 
> > extension Comparable {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: ClosedRange\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self \> range.upperBound {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.upperBound  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if self \< range.lowerBound {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.lowerBound  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > The implementation of clamped(to:) as an extension on Strideable would be  
> > confined to cases where the stride is of type Integer. The implementation  
> > would be as follows:
> > 
> > extension Strideable where Stride: Integer {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source)  
> > compatibility
> > 
> > This feature is purely additive; it has no effect on source compatibility.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect)  
> > on ABI stability
> > 
> > This feature is purely additive; it has no effect on ABI stability.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect)  
> > on API resilience
> > 
> > The proposed function would become part of the API but purely additive.
> > 
> > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives)  
> > considered  
> > Aside from doing nothing, no other alternatives were considered.  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![jadengeller](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jadengeller/32/11544_2.png) [@jadengeller](https://forums.swift.org/u/jadengeller)\
**Post date:** [March 16, 2017, 2:57am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/10 "2017-03-16T02:57:12Z")

</div>

I would suggest using guard. It is more idiomatic Swift for something that “fails out”.

Also, I think this is a bad error message. The `Range` was already created! There was no problem forming it. It was passed as the argument, no problem at all. The problem is trying to \*clamp\* to an empty range, not forming an empty range. I would rephrase it to say something like "Cannot clamp to an empty range”. No reason to redefine what an empty range is by mentioning `upperBound \< lowerBound`.

Cheers,  
Jaden Geller

> **···**
>
> > On Mar 15, 2017, at 7:33 PM, Nicholas Maccharoli \<nmaccharoli@gmail.com\> wrote:
> > 
> > Right, there were a few things missing!  
> > Thanks so much for pointing them out everyone.
> > 
> > Dave - Great idea! I have updated the motivation section section as you suggested!
> > 
> > Neil - Yes I also think the wording could be a bit better but since the word `clamped` is already being used  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I thought I would keep it consistent.
> > 
> > Sean - Looks as if its a term of art to me as well.
> > 
> > Nate,
> > 
> > Good catch! Yes I also thing clamping on an empty range should be a fatal error as well.  
> > An empty range is impossible to create with `ClosedRange` so I left the implementation  
> > of that alone, but it is possible with `Range` so I updated the extension on `Strideable` like so:
> > 
> > extension Strideable where Stride: Integer {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if range.isEmpty { fatalError("Can't form Range with upperBound \< lowerBound") }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > Jaden,
> > 
> > Yeah I think a simple `if` check would work as well.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Maccharoli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicholas_maccharoli/32/6677_2.png) [@Nicholas\_Maccharoli](https://forums.swift.org/u/Nicholas_Maccharoli)\
**Post date:** [March 16, 2017, 2:35am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/11 "2017-03-16T02:35:12Z")

</div>

Here is the updated version of the proposal:

[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md)

Add clamp(to:) to the stdlib

&nbsp;&nbsp;&nbsp;- Proposal: SE-NNNN  
&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md&gt);  
&nbsp;&nbsp;&nbsp;- Authors: Nicholas Maccharoli \<[https://github.com/Nirma&gt](https://github.com/Nirma&gt);  
&nbsp;&nbsp;&nbsp;- Review Manager: TBD  
&nbsp;&nbsp;&nbsp;- Status: Awaiting review

\*During the review process, add the following fields as needed:\*

&nbsp;&nbsp;&nbsp;- Decision Notes: Rationale  
&nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional  
&nbsp;&nbsp;&nbsp;Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
&nbsp;&nbsp;&nbsp;- Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM  
&nbsp;&nbsp;&nbsp;\<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
&nbsp;&nbsp;&nbsp;- Previous Revision: 1  
&nbsp;&nbsp;&nbsp;\<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
&nbsp;&nbsp;&nbsp;- Previous Proposal: SE-XXXX  
&nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/XXXX-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md&gt);

\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#introduction&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction&gt);  
Introduction

This proposal aims to add functionality to the standard library for  
clamping a value to a provided Range. 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.

Swift-evolution thread: Add a clamp function to Algorithm.swift  
\<[The swift-evolution The Week Of Monday 6 March 2017 Archive by thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674&gt);  
\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#motivation&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation&gt);  
Motivation

There have been quite a few times in my professional and personal  
programming life where I reached for a function to limit a value to a given  
range and was disappointed it was not part of the standard library.

There already exists an extension to CountableRange in the standard library  
implementing clamped(to:) that will limit the calling range to that of the  
provided range, so having the same functionality but just for types that  
conform to the Comparable protocol would be conceptually consistent.

Having functionality like clamped(to:) added to Comparable as a protocol  
extension would benefit users of the Swift language who wish to guarantee  
that a value is kept within bounds, perhaps one example of this coming in  
handy would be to limit the result of some calculation between two  
acceptable numerical limits, say the bounds of a coordinate system.  
\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed)  
solution

The proposed solution is to add a clamped(to:) function to the Swift  
Standard Library as an extension to Comparable and Strideable. The function  
would return a value within the bounds of the provided range, if the value  
clamped(to:) is being called on falls within the provided range then the  
original value would be returned. If the value was less or greater than the  
bounds of the provided range then the respective lower or upper bound of  
the range would be returned.

It does not make sense to call clamped(to:) with an empty range, therefore  
calling clamped(to:) and passing an empty range like foo.clamped(to:  
0..\<0) would  
result in a fatal error.

Given a clamped(to:) function existed it could be called in the following  
way, yielding the results in the adjacent comments:

let foo = 100  
// Closed range variant  
foo.clamped(to: 0...50) // 50foo.clamped(to: 200...300) //  
200foo.clamped(to: 0...150) // 100// Half-Open range variant  
foo.clamped(to: 0..\<50) // 49foo.clamped(to: 200..\<300) //  
200foo.clamped(to: 0..\<150) // 100

\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed)  
design

The implementation of clamped(to:) that is being proposed is composed of  
two protocol extensions; one protocol extension on Comparable and another  
on Strideable.

The implementation for clamped(to:) as an extension to Comparable accepting  
a range of type ClosedRange\<Self\>would look like the following:

extension Comparable {  
&nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: ClosedRange\<Self\>) -\> Self {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self \> range.upperBound {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.upperBound  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if self \< range.lowerBound {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.lowerBound  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

The implementation of clamped(to:) as an extension on Strideable would be  
confined to cases where the stride is of type Integer. The implementation  
would be as follows:

extension Strideable where Stride: Integer {  
&nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if range.isEmpty { fatalError("Can't form Range with  
upperBound \< lowerBound") }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source)  
compatibility

This feature is purely additive; it has no effect on source compatibility.  
\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect)  
on ABI stability

This feature is purely additive; it has no effect on ABI stability.  
\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect)  
on API resilience

The proposed function would become part of the API but purely additive.  
\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives)  
considered

Aside from doing nothing, no other alternatives were considered.

> **···**
>
> On Thu, Mar 16, 2017 at 11:33 AM, Nicholas Maccharoli \<nmaccharoli@gmail.com \> wrote:
> 
> > Right, there were a few things missing!  
> > Thanks so much for pointing them out everyone.
> > 
> > Dave - Great idea! I have updated the motivation section section as you  
> > suggested!
> > 
> > Neil - Yes I also think the wording could be a bit better but since the  
> > word `clamped` is already being used  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I thought I would keep it consistent.
> > 
> > Sean - Looks as if its a term of art to me as well.
> > 
> > Nate,
> > 
> > Good catch! Yes I also thing clamping on an empty range should be a fatal  
> > error as well.  
> > An empty range is impossible to create with `ClosedRange` so I left the  
> > implementation  
> > of that alone, but it is possible with `Range` so I updated the extension  
> > on `Strideable` like so:
> > 
> > extension Strideable where Stride: Integer {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if range.isEmpty { fatalError("Can't form Range with upperBound \< lowerBound") }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > Jaden,
> > 
> > Yeah I think a simple `if` check would work as well.
> > 
> > On Thu, Mar 16, 2017 at 10:19 AM, Jaden Geller \<jaden.geller@gmail.com\> \> wrote:
> > 
> > > On Mar 15, 2017, at 12:40 PM, Nate Cook via swift-evolution \< \>\> swift-evolution@swift.org\> wrote:
> > > 
> > > Hi Nick,
> > > 
> > > I think this might have come up in an earlier thread, but clamped(to:)  
> > > with a half-open range argument brings up the question of how to clamp to  
> > > an empty range. As proposed, the implementation of this method would result  
> > > in an error when passed an empty range:
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;foo.clamped(to: 0..\<0)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;// fatal error: Can't form Range with upperBound \< lowerBound
> > > 
> > > Ahh, good catch! The implementation ought to add something like:
> > > 
> > > guard !range.isEmpty else { fatalError("Cannot clamp to an empty range")  
> > > }
> > > 
> > > I don’t think that this is problematic. Ideally we could express in the  
> > > type-system that the range must not be empty, but it is definitely not  
> > > worthwhile creating a `NonemptyRange` type.
> > > 
> > > Is that the desired behavior? Providing documentation for both new  
> > > methods would help make it clear what the expected functionality is.
> > > 
> > > Thanks!  
> > > Nate
> > > 
> > > On Mar 13, 2017, at 9:16 PM, Nicholas Maccharoli via swift-evolution \< \>\> swift-evolution@swift.org\> wrote:
> > > 
> > > Swift-Evolution,
> > > 
> > > After considering the indispensable feedback from the community here is a  
> > > slightly more polished draft.
> > > 
> > > If anyone is interested the draft can be downloaded here:  
> > > [https://github.com/Nirma/swift-evolution/blob/clamp\_function](https://github.com/Nirma/swift-evolution/blob/clamp_function)  
> > > /proposals/NNNN-add-clamp-function.md
> > > 
> > > Thank you so much,
> > > 
> > > - Nick
> > > 
> > > Add clamp(to:) to the stdlib
> > > 
> > > &nbsp;&nbsp;&nbsp;- Proposal: SE-NNNN  
> > > &nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md&gt);  
> > > &nbsp;&nbsp;&nbsp;- Authors: Nicholas Maccharoli \<[https://github.com/Nirma&gt](https://github.com/Nirma&gt);  
> > > &nbsp;&nbsp;&nbsp;- Review Manager: TBD  
> > > &nbsp;&nbsp;&nbsp;- Status: Awaiting review
> > > 
> > > \*During the review process, add the following fields as needed:\*
> > > 
> > > &nbsp;&nbsp;&nbsp;- Decision Notes: Rationale  
> > > &nbsp;&nbsp;&nbsp;\<[https://lists.swift.org/pipermail/swift-evolution/&gt;](https://lists.swift.org/pipermail/swift-evolution/&gt;), Additional  
> > > &nbsp;&nbsp;&nbsp;Commentary \<[https://lists.swift.org/pipermail/swift-evolution/&gt](https://lists.swift.org/pipermail/swift-evolution/&gt);  
> > > &nbsp;&nbsp;&nbsp;- Bugs: SR-NNNN \<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-NNNN&gt;), SR-MMMM  
> > > &nbsp;&nbsp;&nbsp;\<[Issues · apple/swift-issues · GitHub](https://bugs.swift.org/browse/SR-MMMM&gt);  
> > > &nbsp;&nbsp;&nbsp;- Previous Revision: 1  
> > > &nbsp;&nbsp;&nbsp;\<[https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt](https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md&gt);  
> > > &nbsp;&nbsp;&nbsp;- Previous Proposal: SE-XXXX  
> > > &nbsp;&nbsp;&nbsp;\<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/XXXX-filename.md&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md&gt);
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#introduction&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction&gt);  
> > > Introduction
> > > 
> > > This proposal aims to add functionality to the standard library for  
> > > clamping a value to a ClosedRange. 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.
> > > 
> > > Swift-evolution thread: Add a clamp function to Algorithm.swift  
> > > \<[The swift-evolution The Week Of Monday 6 March 2017 Archive by thread](https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674&gt);
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#motivation&gt](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation&gt);  
> > > Motivation
> > > 
> > > There have been quite a few times in my professional and personal  
> > > programming life where I reached for a clampedfunction and was  
> > > disappointed it was not part of the standard library.
> > > 
> > > Having functionality like clamped(to:) added to Comparable as a protocol  
> > > extension would benefit users of the Swift language who wish to guarantee  
> > > that a value is kept within bounds.
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution&gt;Proposed)  
> > > solution
> > > 
> > > The solution proposed is simply that there be a clamped(to:) function  
> > > added to the Swift Standard Library. The function would behave much like  
> > > its name describes.
> > > 
> > > Given a clamped(to:) function existed it could be called in the  
> > > following way, yielding the results in the adjacent comments:
> > > 
> > > let foo = 100  
> > > // Closed range variant  
> > > foo.clamped(to: 0...50) // 50foo.clamped(to: 200...300) // 200foo.clamped(to: 0...150) // 100// Half-Open range variant  
> > > foo.clamped(to: 0..\<50) // 49foo.clamped(to: 200..\<300) // 200foo.clamped(to: 0..\<150) // 100
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design&gt;Detailed)  
> > > design
> > > 
> > > The implementation of clamped(to:) that is being proposed is composed of  
> > > two protocol extensions; one protocol extension on Comparable and  
> > > another on Strideable.
> > > 
> > > The implementation for clamped(to:) as an extension to Comparable accepting  
> > > a range of type ClosedRange\<Self\>would look like the following:
> > > 
> > > extension Comparable {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: ClosedRange\<Self\>) -\> Self {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self \> range.upperBound {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.upperBound  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else if self \< range.lowerBound {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return range.lowerBound  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > }
> > > 
> > > The implementation of clamped(to:) as an extension on Strideable would  
> > > be confined to cases where the stride is of type Integer. The  
> > > implementation would be as follows:
> > > 
> > > extension Strideable where Stride: Integer {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > }
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility&gt;Source)  
> > > compatibility
> > > 
> > > This feature is purely additive; it has no effect on source compatibility.
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability&gt;Effect)  
> > > on ABI stability
> > > 
> > > This feature is purely additive; it has no effect on ABI stability.
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience&gt;Effect)  
> > > on API resilience
> > > 
> > > The proposed function would become part of the API but purely additive.
> > > 
> > > \<[https://github.com/Nirma/swift-evolution/blob/clamp\_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives](https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered&gt;Alternatives)  
> > > considered  
> > > Aside from doing nothing, no other alternatives were considered.  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Nicholas\_Maccharoli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicholas_maccharoli/32/6677_2.png) [@Nicholas\_Maccharoli](https://forums.swift.org/u/Nicholas_Maccharoli)\
**Post date:** [March 16, 2017, 3:21am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/12 "2017-03-16T03:21:53Z")

</div>

Jaden,

Yes that error message is not so great.  
As for the use of guard `if someCondition { fatalError(...)}` seems to be  
a common way of phrasing fatal errors  
in the standard library but guard works just as well.

I updated the proposal to have the following definition:

extension Strideable where Stride: Integer {  
&nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guard !range.isEmpty { fatalError("Can not clamp to an empty  
range.") }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

- Nick

> **···**
>
> On Thu, Mar 16, 2017 at 11:57 AM, Jaden Geller \<jaden.geller@gmail.com\> wrote:
> 
> > On Mar 15, 2017, at 7:33 PM, Nicholas Maccharoli \<nmaccharoli@gmail.com\> \> wrote:
> > 
> > Right, there were a few things missing!  
> > Thanks so much for pointing them out everyone.
> > 
> > Dave - Great idea! I have updated the motivation section section as you  
> > suggested!
> > 
> > Neil - Yes I also think the wording could be a bit better but since the  
> > word `clamped` is already being used  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I thought I would keep it consistent.
> > 
> > Sean - Looks as if its a term of art to me as well.
> > 
> > Nate,
> > 
> > Good catch! Yes I also thing clamping on an empty range should be a fatal  
> > error as well.  
> > An empty range is impossible to create with `ClosedRange` so I left the  
> > implementation  
> > of that alone, but it is possible with `Range` so I updated the extension  
> > on `Strideable` like so:
> > 
> > extension Strideable where Stride: Integer {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if range.isEmpty { fatalError("Can't form Range with upperBound \< lowerBound") }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > Jaden,
> > 
> > Yeah I think a simple `if` check would work as well.
> > 
> > I would suggest using guard. It is more idiomatic Swift for something that  
> > “fails out”.
> > 
> > Also, I think this is a bad error message. The `Range` was already  
> > created! There was no problem forming it. It was passed as the argument, no  
> > problem at all. The problem is trying to \*clamp\* to an empty range, not  
> > forming an empty range. I would rephrase it to say something like "Cannot  
> > clamp to an empty range”. No reason to redefine what an empty range is by  
> > mentioning `upperBound \< lowerBound`.
> > 
> > Cheers,  
> > Jaden Geller

---

<div class="post-metadata">

**Author:** ![jadengeller](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jadengeller/32/11544_2.png) [@jadengeller](https://forums.swift.org/u/jadengeller)\
**Post date:** [March 16, 2017, 3:23am UTC](https://forums.swift.org/t/draft-add-clamped-to-to-the-stdlib/5442/13 "2017-03-16T03:23:41Z")

</div>

> Jaden,
> 
> Yes that error message is not so great.  
> As for the use of guard `if someCondition { fatalError(...)}` seems to be a common way of phrasing fatal errors  
> in the standard library but guard works just as well.

I’d guess that’s because most of the standard library predates the existence of `guard`, but I’m not sure! Either spelling is probably fine (and more the topic of a pull request than a SE review 🙂).

> I updated the proposal to have the following definition:
> 
> extension Strideable where Stride: Integer {  
> &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;guard !range.isEmpty { fatalError("Can not clamp to an empty range.") }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> }
> 
> - Nick
> 
> > Right, there were a few things missing!  
> > Thanks so much for pointing them out everyone.
> > 
> > Dave - Great idea! I have updated the motivation section section as you suggested!
> > 
> > Neil - Yes I also think the wording could be a bit better but since the word `clamped` is already being used  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I thought I would keep it consistent.
> > 
> > Sean - Looks as if its a term of art to me as well.
> > 
> > Nate,
> > 
> > Good catch! Yes I also thing clamping on an empty range should be a fatal error as well.  
> > An empty range is impossible to create with `ClosedRange` so I left the implementation  
> > of that alone, but it is possible with `Range` so I updated the extension on `Strideable` like so:
> > 
> > extension Strideable where Stride: Integer {  
> > &nbsp;&nbsp;&nbsp;&nbsp;func clamped(to range: Range\<Self\>) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if range.isEmpty { fatalError("Can't form Range with upperBound \< lowerBound") }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return clamped(to: range.lowerBound...(range.upperBound - 1))  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > Jaden,
> > 
> > Yeah I think a simple `if` check would work as well.
> 
> I would suggest using guard. It is more idiomatic Swift for something that “fails out”.
> 
> Also, I think this is a bad error message. The `Range` was already created! There was no problem forming it. It was passed as the argument, no problem at all. The problem is trying to \*clamp\* to an empty range, not forming an empty range. I would rephrase it to say something like "Cannot clamp to an empty range”. No reason to redefine what an empty range is by mentioning `upperBound \< lowerBound`.
> 
> Cheers,  
> Jaden Geller

Cheers,  
Jaden Geller

> **···**
>
> > On Mar 15, 2017, at 8:21 PM, Nicholas Maccharoli \<nmaccharoli@gmail.com\> wrote:  
> > On Thu, Mar 16, 2017 at 11:57 AM, Jaden Geller \<jaden.geller@gmail.com \<mailto:jaden.geller@gmail.com\>\> wrote:
> > 
> > > On Mar 15, 2017, at 7:33 PM, Nicholas Maccharoli \<nmaccharoli@gmail.com \<mailto:nmaccharoli@gmail.com\>\> wrote:
