Join the DZone community and get the full member experience.Join For Free
from the moment apple has announced swift, the new ios and os x programming language, the web is full of hate and praise, constructive criticism and pointless rants, confusion and excitement — and many of these boil down to “why swift?” — namely, why apple chose to design a new programming language rather than pick and adapt an existing one.
needless to say, i don’t work for apple, so all i can offer is an educated guess based on a lot of playing with swift and trying to understand the mindset that led to its design and implementation. if you have your own opinion, i’d love to hear it in the comments or — even better — in your own blog post.
swift design goals
when you see a new programming language, you must ask what its design goals have been. swift is designed to replace objective-c, but that’s not a goal in and of itself. there are many flaws with objective-c and many ways to “fix” them, so we must be more specific. also, saying swift is a more modern language is not enough. we need to point out how it is more modern and what we get out of it. so, here’s my attempt at the design goals of swift:
1. performance and runtime
swift must compile down to native code with no runtime compilation or interpretation . swift must be able to match and exceed the performance of optimized objective-c code without requiring an expensive runtime or a full-blown garbage collector. the reason for the latter requirement is that swift must compete in the ios world, where devices are memory-restricted and cannot afford garbage collection or costly runtime services. the security architecture of these devices also precludes — or at least makes undesirable — just-in-time compilation.
2. interop with objective-c
swift must make it trivial to interact with existing objective-c code . this means it must be easy for swift code to call objective-c code, and it must be possible (and ideally easy as well) for objective-c code to call swift code. the interop should not just be on the module level — it should be possible for a swift class to implement an objective-c protocol, or for a swift class to extend an objective-c class through either an extension or inheritance.
3. type safety
ten years of objective-c in the mainstream must have proven that its dynamic, loosely-typed nature isn’t a great idea for the robustness users tend to expect from mobile apps. (hey, don’t crucify me over this last statement; while it is possible to write robust code in a non-type-safe language, i personally believe it is easier to do so in a type-safe language.) it’s also not very good for performance: objective-c method calls are always bound at runtime and go through a messaging infrastructure that precludes compiler optimizations such as inlining and makes it very difficult to perform whole-program optimization.
for example, i never saw a good reason for putting objects of multiple types into the same nsarray or nsdictionary. but hey, the language allows us, so you tend to drift with the wind. i also saw no reason for delegate hosts to use @optional methods and canperformselector: instead of requiring the delegate to implement whatever methods are required, but with this design forced on you at every opportunity (with the simplest examples being uitableviewdatasource and uitableviewdelegate), you end up adopting it as well.
swift, then, must be type-safe . collections of “any object” and properties “of any type” should be the exception, not the rule. there must be facilities for strongly-typed generic code and for compile-time safety of variable assignments.
4. better nil handling
if you ever programmed in objective-c, you know it has a weird way about nil handling. when you call a method on a nil pointer, it just silently does nothing and returns nil (or 0, or no — depending on what your method is supposed to return). although there are some situations where you can actually take advantage of this, at some point it will bite you.
swift must do better where nil handling is concerned . there should be a clear way to express that a variable cannot be nil, and the compiler should be able to verify it. when variables can be nil, they must be explicitly tested for nil before they can be used — and again the compiler should be able to verify it.
5. unification and modernization of function types
function types in swift must be first-class citizens . it should be possible — and easy! — to pass functions to other functions, to return functions from other functions, and to create closures with a simple syntax and intuitive behavior.
objective-c, by the way, comes pretty close with its blocks syntax, but it has a few setbacks: first, you must mark mutable closed variables explicitly (with the __block keyword); second, the way to avoid reference cycles between an object that references a block and the block itself is pretty ugly; third, blocks are not equivalent to function pointers and cannot be used interchangeably with function pointers.
6. memory management and memory safety
swift must make memory management automatic and safe . developers should not have to explicitly delete memory that they allocated, and should have to write explicit deinit methods infrequently. swift must also make it very hard (if not impossible) to perform unsafe memory accesses, such as pointer arithmetic and unsafe pointer casts (such as integer to pointer conversion).
again, objective-c with arc (automatic reference counting) goes a long way in terms of memory management, but is still full of potential holes for unsafe memory accesses.
7. easy transition path from objective-c
swift should make objective-c developers feel at home , at least around the fundamental concepts such as: the type system, classes, inheritance, initialization, protocols, and other language features. it should do so without compromising on the other design goals whenever possible.
there are of course additional design goals that i am not listing here, which are implicit in the language as we see it today. the strong pattern-matching capability on enums and other types is one of my favorite swift features, and is clearly “borrowed” from modern functional languages. operator overloading and custom operators is another example. but i consider these to be mostly convenience features, and not a pillar of the language, despite their usefulness.
- so, what’s wrong with xyz?
with that said, why not simply use an existing programming language? setting aside any potential concerns about copyrights, apple could pick any existing programming language and perform minimal adaptation to make it suitable for use with ios and os x. the potential benefits would include 1) tapping into an existing developer community, 2) not spending as much time working on the language design and syntax, 3) possibly allowing existing code reuse from the developer community, 4) making it easier to develop cross-platform applications that run on ios and other platforms.
but when you try to come up with an actual mainstream language that would be suitable for this, the design goals we discussed above might have some strong objections.
for example, consider c++. a mainstream language, no doubt, with existing cross-platform support for ios, android, windows phone, windows, os x, linux, microwaves, electric bicycles, and whatnot. apple’s compiler toolchain even supports c++ today — you can go ahead and use it on os x or ios. my biggest objections to c++ today are memory safety and the unprecedented complexity of the language. even though it’s possible to be productive in modern c++, the opportunities for getting something wrong are so common that i would hate to see c++ as the standard for ios and os x development.
next, consider java. i’ll even be willing to forgive its verbosity and sometimes-plain-ugliness, but can it be married with the design goals above? i think not. compiling java down to native code with no garbage collection and no costly runtime means getting rid of a very large part of the language and its runtime features, producing a dialect that will be largely incompatible with any existing java code (and thus missing the goal of using an existing language). additionally, adapting java to make it compatible with existing objective-c code would require introducing several non-trivial language extensions to support categories, function types, protocols (which are not quite the same as interfaces), and custom value types. again, it wouldn’t quite be java after it’s been steamrolled by objective-c. any potential for existing code reuse, cross-platform compability, and tapping into an existing developer community would be lost if the language undergoes significant adaptation.
finally, how about c#? xamarin has been very successful compiling c# down to native code and running with a minimal runtime layer on ios — although still with garbage collection services. xamarin have also set a standard for adapting objective-c language features to c# — exposing protocols as interfaces, action methods as event handlers, and so on. c# still doesn’t fully deliver on some of the design goals above (such as a stronger stance on nil handling and pattern matching), but perhaps that could have been baked into the language with certain perturbations.
but in case you haven’t noticed, apple has a very strong stance against full garbage collection on mobile devices (and perhaps rightly so). it might be worth the thought experiment to work on removing garbage collection support from c# and replacing it with reference counting. there is no fundamental problem with that, except for the additional language support that would be required for conveniently expressing weak/non-owning references. the result of these adaptations would again be a dialect of c#, incompatible with the “mainstream c#” we know today.
how many ios developers actually speak c#, however? would it really be easier for them to transition from objective-c to c# than to swift? would introducing a different dialect of c# that would be incompatible with “mainstream c#” be easier for existing c# developers than forcing them to learn a new language? these are interesting questions. i personally prefer a new language with an almost-clear slate to an incompatible dialect of another language. with both microsoft and apple pulling c# in two different directions, the end result could be — effectively — two different languages, anyway.
i realize many of the points above are controversial, and as i said in the very beginning, all i’m offering here is my opinion. i know one thing for sure after having used swift for less than a week, and it’s that i unequivocally prefer it to objective-c (and that’s from a developer who got used to objective-c’s quirks very quickly!). if swift’s net effect is that ios and os x developers can be more productive in a language that’s type-safe, memory-safe, and interoperable with existing apis, then i’m pretty happy even if it’s not a language that i knew a week ago.
Published at DZone with permission of Sasha Goldshtein, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.