I like programming in Swift, but the language designers made one choice that I disagree with. In fact, I strongly disagree with it. Here are two sets of code that do the same thing (assuming Swift were to allow the second bit of code to work). Which one of these actually makes programming less error-prone and more understandable?

let start = self.index(self.startIndex, offsetBy: startIndex)
let end = self.index(self.startIndex, offsetBy: endIndex)
return self[start..<end]
return self[startIndex..<endIndex]

The first set of code is three lines and creates two new values. The second set of code (the one line) should do the same thing but is impossible in Swift due to a weird decision by the designers. You cannot use an integer to pick a character position in strings. The explanations always start with a discussion about how strings are stored with variable-length characters, which is a bad argument since the programmer should not care how the string data is stored internally. Then, there is some sort of argument that a subscript, the thing between the square brackets, should always take a fixed amount of time to work. This is strange since Swift allows programmers to extend existing classes and create their own classes using custom subscript operators. If this reason was valid, Swift would never allow a custom subscript operator. One final explanation is that strings are made of bytes and characters, and it’s unclear if a subscript should apply to the bytes or the characters. Since the Swift designers could not decide what operation would be most common, and let’s face it, it’s fairly obvious, there would be no subscripting with integers for strings. Ultimately, the internet is full of examples of people just implementing their own subscript functions for strings. In every single case, they index using characters, not bytes!

I think that the Swift designers did a lot to reduce the amount of code one must write to get things done. They even made the compiler assume “return” in certain circumstances so you could leave off that one word in your code. But they also decided to make the string handling as ugly and hard to read as they could get away with using reasoning that just holds no weight for me. I want to write the fewest lines of code to get something done because going back years later and reading that code will be hard enough without looking past a bunch of code that simply converts integers to “string indexes” with the exact same meaning.

They actually made this same mistake somewhere else. The compiler will not convert between data types when every other language seems to do fine doing so. For instance, if you write code that looks like this, you will get an error:

let a = 1
let b = 2.0
let c = b / a

The code will fail to compile because a is an integer and b is a floating point value. In math class, no one ever cared about this. Why would a programming language force the programmer to write in the conversion when they will always write the same conversion every time? What is gained by converting the int value to a Double value like this:

let c = b / Double( a )

The math result is the same. Could there be a loss of data? Well, the conversion of ‘a’ to Double doesn’t solve that problem, does it? So again, the Swift designers decided to make me write more code, not less, to do something simple and obvious.

I’m writing some code right now that indexes characters in a string, and I can’t figure out why my extensions in the String class are not working. It seems as if something in the language is detecting that I’m trying to work around their annoying lack of cleverness and doing something to stop it.

[End of Rant]