So that’s it, Apple introduced its new programming language: Swift. Here are some thoughts after reading the publicly available documentation:
Of course it’s based on the Objective-C runtime, so it can access Cocoa APIs, but interestingly it goes beyond that with interesting struct semantics.
It makes me think of Rust a lot. It definitly isn’t Rust (no move semantics, no borrowed references), but there’s many similarities in the syntax.
There’s no pointer type at all. Use arrays, dictionaries and objects if you need to put things on the heap.
Done with null pointers: non-nullable is the default and “optional” can be added to the type with “?”. That’s something I’ve gone to great lenghts to mimick using C++, with limitted success. I’m quite happy to see a language with that built-in.
Generics. That was overdue in Cocoa.
No exceptions. Not completly surprising as Cocoa use exceptions only for logic errors in the program. It thus makes some sense to get rid of all the exception handling logic boilerplate the compiler has to insert everywhere for ARC.
I wonder if a new error handling pattern will emerge based on those
enums to replace the current
NSError pointer to autoreleased pointer used all over the place by Cocoa.
Apple says it’s designed for safety. It’s important to note that the design of Swift doesn’t protect prevent memory corruption that could happen due to multi-threaded code at all however. I find it striking that there’s no mention of threads or concurrency at all in the language documentation. Maybe they’ll just outlaw threads, but that’d be surprising.
Beside this multithreaded hole, the rest of the language seems to be safe from memory corruption bugs.
There’s no way to temporarily escape the safeties (like Rust’s
unsafe block), so any unsafe thing you may want to do will have to be in C or Objective-C.
Will this be open-source?
I’ll definitly give it a try. I’ll probably use it from now on to write most Cocoa stuff.
I’m a little disapointed because when they wrote “designed for safety” I was hoping the safety would cover threads, like in Rust, but that’s not the case. But that’s not something that’ll to prevent me from using it, we’ll just have be careful when playing with threads, as always.