[swift-evolution] [Accepted] SE-0172: One-sided Ranges

Haravikk swift-evolution at haravikk.me
Tue Apr 25 10:55:01 CDT 2017


While it's good that this was accepted I still feel there could be some more discussion about whether the operators should be prefix/postfix or instead involve a more explicit declaration of one-sidedness.

I still would very much prefer that the operator declarations were binary and take Void as a second argument, this way there is very explicit indication that one-sidedness was requested, rather than potentially accidental; assuming many subscripts and methods that take currently closed ranges will be updated to also take one-sided ranges, the distinction is very important as a omitting one of the values could represent a mistake, and result in one-sided behaviour with unintended consequences.

With a Void "open" argument this would look like:

func ... <T:Comparable>(lhs:T, rhs:Void) -> RangeFrom { ... }
func ... <T:Comparable>(lhs:Void, rhs:T) -> RangeTo { ... }

let rangeFrom = 5 ... ()
let rangeTo = () ... 5

Not the prettiest with the Void brackets, however, if we could in future get underscore as another alias for Void we could refine this into:

let rangeFrom = 5 ... _
let rangeTo = _ ... 5

This would have consistency with other uses of underscore that are used to indicate that something is being ignored on purpose, which I think fits this proposal very well. It would also leave the prefix/postfix ellipsis operator free for use on something else with less chance of creating ambiguity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170425/f08d5781/attachment.html>


More information about the swift-evolution mailing list