[swift-evolution] [draft] Add `clamped(to:)` to the stdlib

Haravikk swift-evolution at haravikk.me
Wed Mar 15 17:15:13 CDT 2017


> On 15 Mar 2017, at 19:40, Nate Cook via swift-evolution <swift-evolution at 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:
> 
>     foo.clamped(to: 0..<0)
>     // 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.

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170315/37407c38/attachment.html>


More information about the swift-evolution mailing list