[swift-evolution] [Idea] Add `bounds` function to standard library

Karl razielim at gmail.com
Thu Sep 1 23:32:11 CDT 2016


> On 2 Sep 2016, at 06:14, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 
> Run loop modes are named by string, and as you can see in your link, comparisons of run loop modes are by their raw value--i.e. by string. While it's of course sensible to have a total ordering for strings, I'm skeptical that you would typically want to get the "maximum" of two strings, and I'm not aware of a use case for clamping a string to a range of strings.
> 
> If you're really doing that in your code, you should be aware that the default ordering for String (or was it just NSString?--my memory is hazy now) behaves differently on OS X and Linux, at least as of a few months ago. You really should be using string-specific comparison methods (either case-sensitive or not) on appropriately normalized strings, in my opinion.

I think their implementation is wrong — comparing runloop modes by their raw values doesn’t make sense if they are strings. But Runloop is patchy anyway, lots of it still lives in CoreFoundation. I was trying to draw attention to the concept rather than the impl.

Basically, my point is that there are other abstract types other than numbers which can be compared and clamped. A “priority” enum would be an example. Sure, you could make it RawRepresentable and compare/clamp the raw values, but that sounds like a workaround rather than a feature. It may be trivial to implement, but so are min/max themselves.

There’s obviously a line as to what should/shouldn’t be in the standard library. I’m not sure the line is so meandering that it includes min/max while excluding clamp, but it’s all a bit arbitrary anyway ¯\_(ツ)_/¯


More information about the swift-evolution mailing list