News for hackers. Stuff that matters.
Re:Why Is Swift's String API So Hard? by ex3ndr on Friday November 06, 2015 @11:25AM (#10520265)
the main problem with swift strings is actually naming. You don't have simple method

replace(src:String, dst:String)

but you have weird

stringByReplacingOccurrencesOfString(src, withString: dest, options: NSStringCompareOptions(), range: nil)

Why not to have simple convenience method?

Re:Why Is Swift's String API So Hard? by alexbock on Friday November 06, 2015 @11:41AM (#10520352)
Aside from the fact that they carried that style of naming over from Objective-C, the one benefit I see is that it makes it obvious that it returns the result in a new string rather than modifying the object itself. In C++ you would need to look at the signature to tell which way a method named replace worked, e.g.

void A::replace(const A& source, const A& dest);

vs

A A::replace(const A& source, const A& dest) const;

That said, I think you can get this same benefit without such a verbose name. Perhaps something like "withReplacement"?

Re:Why Is Swift's String API So Hard? by cballard on Friday November 06, 2015 @11:45AM (#10520376)
A solution to this would be to disallow mutable value types. Then, all methods would _have_ to return a new instance.

The compiler implementation could use mutability behind the scenes for efficiency, while the language exclusively allowed immutable values.

Re:Why Is Swift's String API So Hard? by cballard on Friday November 06, 2015 @11:43AM (#10520361)
That is not a Swift method, it's an Objective-C method on NSString, which Swift String can be silently converted to.
Re:Why Is Swift's String API So Hard? by zw on Friday November 06, 2015 @12:06PM (#10520481)
Not anymore; those methods are implemented in an extension on `String` that don't always involve bridging back.
Re:Why Is Swift's String API So Hard? by mikeash on Friday November 06, 2015 @12:11PM (#10520514)
They're still only available if you import Foundation. They're just NSString methods with wrappers. The real problem is just that Swift itself doesn't have methods for locating a string in another string, or replacing strings in another string.

This is why I was careful to say that Swift's API is the best in terms of its fundamental design. It still has a lot of missing functionality compared to other languages right now.