<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 10, 2017, at 11:32 AM, Nevin Brackett-Rozinsky via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">On Fri, Mar 10, 2017 at 4:16 AM, David Sweeris via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><br class="">I’m ok with doing it as an extension on `Comparable`, although we should add an overload for regular ranges, too.</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div></div></blockquote><div class=""><br class=""></div><div class="">How would the semantics of that work?</div></div></div></div></div></blockquote><br class=""><div>Good questions! I was mostly thinking about how often we do stuff like “for i in 0 ..< arr.count”, and that it’d be handy to not have to switch over to using ClosedRange just to be able to clamp to it. To answer your questions, though…</div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">Should “16.clamped(to: 0..<10)” produce 9 or 10?</div></div></div></div></div></blockquote><br class=""><div>9</div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">What about “16.clamped(to: 0..<0)”, which is an empty range?</div></div></div></div></div></blockquote><br class=""><div>For `Int`? Crash (which, until about 5 minutes ago, is what I thought would happen if you tried to create a range that’s empty like that). For types that support it, I’d say NaN or something like “nil”/“empty” is the most appropriate return value</div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">Does “16.0.clamped(to: 0..<10)” yield 10.0 or the next-smaller representable Double?</div></div></div></div></div></blockquote><br class=""></div><div>Next-smaller, IMHO. It’s not <i class="">exactly</i> semantically correct, but AFAIK that’s as correct as Float/Double can be.</div><div><br class=""></div><div>Mostly though I’d really like to be able to clamp to array indices, which are pretty much always written as a `Range`, rather than a `ClosedRange`. We could write the function for `Range` to only be generic over `Comparable&Integer`, if the floating point corner cases are too much.</div><div><br class=""></div><div>- Dave Sweeris</div></body></html>