On Jun 5, 2014, at 12:07 AM, Chris Lattner < email@hidden> wrote: On Jun 3, 2014, at 8:19 PM, Jens Alfke < email@hidden> wrote: On Jun 3, 2014, at 2:16 PM, Ron Hunsinger < email@hidden> wrote: - In Swift, a..b includes a and excludes b; a...b includes both endpoints. - In Ruby, it's exactly the opposite. a..b includes both endpoints; a...b excludes b.
Oh, weird. I remembered the Ruby range operators when I read about Swift’s and assumed Ruby was the inspiration; but then why do them the other way around?
(But to me, it makes more sense that three dots would give you a bigger range than two dots. Shrug.)
The Swift approach is easy to remember: one more dot gives you one more value.
A mnemonic like this is useless unless it always leads to the correct answer.
I'm writing some code. I need one of the range operators. I know whether I want the last value to be included, but do I use two dots or three? There's this mnemonic that tells me, but is this the language where that mnemonic works?
And that's the real heart of the problem. I have this mnemonic. It works in Ruby, but not in Swift. Or is it the other way around? I need yet another mnemonic to remind me which language the first mnemonic works in.
The .. operator to create a range first appeared (IIRC) in Pascal, where it could be used to express a subtype of an enumeration. (Integer types are technically an enumeration, as is something like (sun, mon, tue, wed, thu, fri, sat).) If .. omitted either endpoint, you could not write a sub-range that included that endpoint of the underlying type.
Ruby has a .. operator. For consistency with Pascal, .. has the same meaning in Ruby as in Pascal. A half-open range is also useful, especially when iterating over an array or slicing a string, so Ruby introduces a new ... operator for this new semantic.
The question isn't whether more dots should include more values. The question is whether to confuse programmers who routinely work with more than one language. Reversing the meanings does that. It's a bad idea, no matter how well-intentioned.
-Ron Hunsinger
|