{"id":2865,"date":"2014-06-22T14:54:14","date_gmt":"2014-06-22T17:54:14","guid":{"rendered":"http:\/\/brockerhoff.net\/blog\/?p=2865"},"modified":"2014-06-22T20:51:16","modified_gmt":"2014-06-22T23:51:16","slug":"swift-first-impressions","status":"publish","type":"post","link":"https:\/\/brockerhoff.net\/blog\/2014\/06\/22\/swift-first-impressions\/","title":{"rendered":"Swift: First Impressions"},"content":{"rendered":"<p>Over\u00a0two weeks ago, Apple at <a href=\"https:\/\/developer.apple.com\/wwdc\/\" target=\"_blank\">WWDC<\/a> announced something entirely unexpected: thousands of new APIs and a brand-new programming language, Swift. No hardware, of course; it&#8217;s a <em>developers<\/em> conference, remember?<\/p>\n<p>Reactions varied all over the spectrum. Non-developers (especially \u201cindustry analysts\u201d) mostly had <a href=\"http:\/\/www.macworld.com\/article\/2358461\/i-dont-get-it.html\" target=\"_blank\">no idea what it meant<\/a>: they said Apple had announced \u201c<a href=\"http:\/\/247wallst.com\/technology-3\/2014\/06\/02\/apple-announces-nothing-at-developer-conference\/\" target=\"_blank\">nothing<\/a>\u201d. Almost all\u00a0developers, however, were ecstatic \u2014 \u201c<a href=\"http:\/\/www.asymco.com\/2014\/06\/10\/the-greatest-show-on-earth\/\" target=\"_blank\">the most significant event Apple ever staged<\/a>&#8220;. Regarding Swift, this initial enthusiasm diverged as soon as people read the (relatively sparse) documentation and actually began to play around with the language \u2014\u00a0a very early beta version was available for download soon after the announcement. Hilarity, chaos and <a href=\"https:\/\/www.linkedin.com\/today\/post\/article\/20140608104132-11218673-apple-swift-and-the-whining-and-oh-how-they-whine\" target=\"_blank\">pandemonium<\/a> ensued;\u00a0<a href=\"http:\/\/tenser.typepad.com\/tenser_said_the_tensor\/2004\/03\/a_word_of_expla.html\" target=\"_blank\">tension, apprehension and dissension<\/a> had begun.<\/p>\n<p>As usual, almost everybody tried to <a href=\"http:\/\/quoteinvestigator.com\/2014\/05\/08\/hammer-nail\/\" target=\"_blank\">project<\/a> their grievances, expectations and experiences onto the new language. The open-source advocates griped that no source was available. The cross-platform advocates complained that there was no version running\/compiling for Android (as if Apple would have any interest in promoting that!). The Objective-C programmers unsuccessfully tried to translate their code into Swift and complained that there was only limited dynamic dispatching and introspection. The C programmers complained that there were no preprocessor macros and that Swift seemed to be \u201cObjective-C without the C\u201d. The Haskell\/Erlang\/Scala programmers complained that many functional programming facilities were missing, and that the language was \u201ctoo mutable\u201d. The Java programmers\u00a0complained that the language was \u201ctoo C++-like\u201d, but resented the lack of exceptions. The C++ programmers also resented the lack of exceptions and wanted std::somethings. The Type Theorists complained that generics were \u201cnot generic enough\u201d. JavaScript programmers&#8230; well, you get the idea. Almost everybody complained about Array mutability semantics, about missing semicolons and the parsing of whitespace, and (of course) said that the syntax \u201clooked weird\u201d. Serious fights erupted on Twitter, disagreeing on whether Swift was a \u201cmodern\u201d language and what Apple&#8217;s intentions were.<\/p>\n<p>And, as always happens, many people said, in effect, \u201cOMG Apple you&#8217;re soo stooopid WTF fix this now!\u201d. This is the usual symptom of looking at the surface and not understanding what might be happening underneath.<\/p>\n<p style=\"padding-left: 30px;\">Voluminous disclaimer and sidenote with historical digressions:<\/p>\n<p style=\"padding-left: 30px;\"><em>Many of the complaints in the paragraphs above are condensations of what I understood people to be saying and none are meant to be actual live quotes \u2014 which is why I didn&#8217;t link to any specific instance. I&#8217;m not interested in discussing most of these personally right now, thank you.<\/em><\/p>\n<p style=\"padding-left: 30px;\"><em>I&#8217;ve been programming since 1969, \u00a0in C since 1984, in Objective-C since 2000. I wrote only\u00a0<\/em><a href=\"\/aur.html\" target=\"_blank\">one<\/a><em> application in C++ back in the Classic days \u2014 it was pretty much mandatory in the CodeWarrior\/PowerPlant days. I did my CS degree in the early\u00a01970&#8217;s, when \u201cmodern\u201d language still meant <a href=\"http:\/\/en.wikipedia.org\/wiki\/ALGOL_68\" target=\"_blank\">ALGOL 68<\/a>\u00a0&#8211; see the mind-boogling\u00a0<a href=\"http:\/\/www.softwarepreservation.org\/projects\/ALGOL\/report\/Algol68_revised_report-AB.pdf\" target=\"_blank\">official reference<\/a> (<\/em>large PDF<em>).<\/em><\/p>\n<p style=\"padding-left: 30px;\"><em>When <a href=\"http:\/\/www.vintage-computer.com\/byte.shtml\" target=\"_blank\">BYTE Magazine<\/a>&#8216;s special <a href=\"https:\/\/archive.org\/details\/byte-magazine-1981-08\" target=\"_blank\">Smalltalk issue<\/a> came out in 1981, I was very interested, but couldn&#8217;t come to grips with the weird syntax. I bought\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Adele_Goldberg_(computer_scientist)\" target=\"_blank\">Adele Goldberg<\/a>&#8216;s classic books about Smalltalk \u2014\u00a0the <a href=\"http:\/\/stephane.ducasse.free.fr\/FreeBooks\/BlueBook\/Bluebook.pdf\" target=\"_blank\">blue book<\/a>\u00a0(<\/em>large PDF<em>),\u00a0the <a href=\"http:\/\/stephane.ducasse.free.fr\/FreeBooks\/TheInteractiveProgrammingEnv\/TheInteractiveProgrammingEnv.pdf\" target=\"_blank\">orange book<\/a><em>\u00a0(<\/em><\/em>large PDF<em><em>)<\/em>\u00a0and the <a href=\"http:\/\/stephane.ducasse.free.fr\/FreeBooks\/BitsOfHistory\/BitsOfHistory.pdf\" target=\"_blank\">green book<\/a> (<\/em>large PDF<em>) \u2014 and periodically tried to understand them; very difficult without access to a working compiler! In the late 80&#8217;s I put these aside (and, unfortunately, lost them in a move). After Apple acquired NeXT in 1996, I became aware of Objective-C&#8217;s roots in Smalltalk, but didn&#8217;t give it much thought.<\/em><\/p>\n<p style=\"padding-left: 30px;\"><em>Around 2000, restarting my work as an indie developer, I <a href=\"\/xray\/\" target=\"_blank\">started programming<\/a> in Objective-C and Cocoa. As an experienced C programmer I had little difficulty with Objective-C, and quickly got used to\u00a0the nested [[ ]]s. I never wrote a full Carbon app as such. I also never managed to acquire a working Smalltalk compiler, even after a few became available on the Mac. However, a couple of years ago, I found the Smalltalk books in PDF format (as linked above) and was astounded: the formerly opaque things about methods, messages, dynamic dispatching, objects and so forth \u2014 suddenly all was clear and obvious! That&#8217;s the advantage of using-while-learning, at least for me.<\/em><\/p>\n<p style=\"padding-left: 30px;\"><em>Unlike many colleagues I never hesitated to go beyond Cocoa, always\u00a0using CoreFoundation, BSD\/Darwin and a variety of interfaces according to necessity, and once manual memory management became ingrained, tossing objects and buffers back and forth between the various APIs. Except for short utilities for my own use, I haven&#8217;t adopted <\/em><a href=\"http:\/\/en.wikipedia.org\/wiki\/Automatic_Reference_Counting\" target=\"_blank\">ARC<\/a><em> yet \u2014 I found too many edge cases for my established programming habits.<\/em><\/p>\n<p>\u00a0So, back to Swift. It really appears to be\u00a0a very pragmatic language. If you look at the generated library header (in Xcode, command-doubleclick on any Swift type to see it), nearly all operators and types are defined there, in often surprising detail. In other words, few language features are hard-wired into the parser\/compiler &#8211; the Swift library\/runtime and the pre-LLVM optimizer are, instead, responsible for the language and its implementation details, and therefore more easily twiddled\u00a0if necessary.<\/p>\n<p>This is, of course, very convenient for Apple: a\u00a0small team could tinker around with all aspects of Swift while leveraging most of the existing <a href=\"http:\/\/llvm.org\/\" target=\"_blank\">LLVM infrastructure<\/a> and keeping up with the latest changes in iOS and OS X. Indeed, in retrospect, it appears that Swift was even driving many\u00a0of those changes!<\/p>\n<p>Let&#8217;s look at a brief timeline to explain what I mean:<\/p>\n<ul>\n<li>2000-2002: <a href=\"http:\/\/nondot.org\/sabre\/\" target=\"_blank\">Chris Lattner<\/a>&#8216;s <a href=\"http:\/\/llvm.org\/pubs\/2002-12-LattnerMSThesis.html\" target=\"_blank\">masters thesis<\/a>\u00a0on LLVM;<\/li>\n<li>2005: Lattner <a href=\"http:\/\/nondot.org\/sabre\/Resume.html#Apple\" target=\"_blank\">hired by Apple<\/a>; Apple uses LLVM for the OpenGL shading language in Mac OS X 10.5;<\/li>\n<li>2006-2008: Apple introduces experimental\u00a0llvm-gcc in Xcode 3.1; \u201c<a href=\"http:\/\/lists.cs.uiuc.edu\/pipermail\/cfe-dev\/2008-August\/002670.html\" target=\"_blank\">blocks\u201d<\/a>\u00a0and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Grand_Central_Dispatch\" target=\"_blank\">GCD<\/a> appear;<\/li>\n<li>2009: Apple introduces <a href=\"http:\/\/clang.llvm.org\/\" target=\"_blank\">Clang<\/a> as an alternative for gcc; <a href=\"http:\/\/en.wikipedia.org\/wiki\/OpenCL\" target=\"_blank\">OpenCL<\/a>\u00a0and\u00a0Clang <a href=\"http:\/\/clang-analyzer.llvm.org\/\" target=\"_blank\">static analyzer<\/a> appear;<\/li>\n<li>2010: Lattner begins working on Swift; Clang <a href=\"http:\/\/clang.llvm.org\/cxx_status.html\" target=\"_blank\">fully supports C++ <\/a>and llvm-gcc is the default compiler;<\/li>\n<li>2011: gcc\/gdb are discarded, Clang\/lldb are defaults, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Automatic_Reference_Counting\" target=\"_blank\">ARC<\/a>\u00a0introduced in Xcode 4.2;<\/li>\n<li>2012-2013:\u00a0iOS\/OS X are fully built with the new infrastructure, Objective-C literals\u00a0in Xcode 4.4;<\/li>\n<li>2013: Lattner becomes head of the developer tools department;<\/li>\n<li>2014: Swift comes out in Xcode 6.0.<\/li>\n<\/ul>\n<p>The LLVM team (Lattner, <a href=\"http:\/\/awards.acm.org\/award_winners\/cheng_5756445.cfm\" target=\"_blank\">Evan Cheng<\/a> who is also\u00a0at Apple, and <a href=\"http:\/\/awards.acm.org\/award_winners\/adve_3453297.cfm\" target=\"_blank\">Vikram Adve<\/a> of UIUC) also received the <a href=\"http:\/\/awards.acm.org\/software_system\/\" target=\"_blank\">2012 ACM Software System Award<\/a>, and of course, LLVM, Clang, LLDB are open-source projects being driven forward by many people\u00a0who also deserve lots of credit.<\/p>\n<p>Nevertheless, it&#8217;s tempting to see all this as Chris Lattner&#8217;s plan for world domination&#8230; just picture him stroking a white cat and going\u00a0\u201cmwahaha!\u201d \ud83d\ude42 [<strong>Update:<\/strong> Thanks to <a href=\"https:\/\/twitter.com\/darth\/status\/480858299005272064\" target=\"_blank\">@darth<\/a> for the illustration!]<a href=\"https:\/\/twitter.com\/darth\/status\/480858299005272064\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2867\" src=\"\/blog\/wp-content\/uploads\/2014\/06\/mwahaha.jpg\" title=\"Mwahahahaha!\" width=\"514\" height=\"557\" srcset=\"https:\/\/brockerhoff.net\/blog\/wp-content\/uploads\/2014\/06\/mwahaha.jpg 514w, https:\/\/brockerhoff.net\/blog\/wp-content\/uploads\/2014\/06\/mwahaha-276x300.jpg 276w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/a><\/p>\n<p>But really, all this points to progress in Apple&#8217;s platforms being driven by a consistent plan to modernize and implement new technologies everywhere; even hardware was affected, as the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Apple_A6\" target=\"_blank\">Apple A6<\/a> CPU (and no doubt its successors) were designed in parallel with the corresponding LLVM code generator. Similarly, from 2009 forward, software advances\u00a0like ARC, blocks, GCD, runtime modernizations etc. are now seen as preparing the ground for Swift at all levels.<\/p>\n<p style=\"padding-left: 30px;\">Sidenote:<\/p>\n<p style=\"padding-left: 30px;\"><em>A few years ago <a href=\"\/blog\/2010\/05\/21\/re-ipad-time-4\/\" target=\"_blank\">I posted<\/a> about Apple&#8217;s hardware options <a href=\"\/blog\/2010\/04\/15\/re-ipad-time-3\/\" target=\"_blank\">being enabled<\/a> by LLVM, and with the A6 that has indeed begun to happen. Apple&#8217;s in position now to design\u00a0their own CPU and just have to write a new optimizer backend for it \u2014 and switch architectures in new hardware without users, or even developers, noticing any significant change.<\/em><\/p>\n<p style=\"padding-left: 30px;\"><i>When I began studying programming languages and compilers, <\/i><a href=\"http:\/\/en.wikipedia.org\/wiki\/UNCOL\" target=\"_blank\">UNCOL<\/a><i><a href=\"http:\/\/en.wikipedia.org\/wiki\/UNCOL\" target=\"_blank\"> was the holy grail of programming<\/a>:\u00a0\u00a0a universal intermediate language to adapt any high-level language\/compiler to any machine architecture. LLVM is the first implementation of that.<\/i><\/p>\n<p>What does all this mean for Swift? Contrary to what you may hear from some quarters, it&#8217;s not\u00a0an amateurish, ham-fisted\u00a0attempt at locking developers in to Apple&#8217;s \u201cwalled garden\u201d. As Apple has said publicly, it&#8217;s a systems programming language that ties in to key Apple technologies. I don&#8217;t doubt that it&#8217;s already being deployed internally and we can expect to see key low-level frameworks \u2014 Security, dyld, IOKit are candidates which come to mind \u2014 rewritten in Swift as soon as feasible. In the long run, the kernel itself, Core Foundation and others may follow suit; picture \u201cSwiftKit\u201d unifying much of AppKit and UIKit. Making Swift available to developers at this beta stage is good policy but probably not Apple&#8217;s primary focus.<\/p>\n<p>But, you may ask, why not use C++ or the hybrid Objective-C++? Why not use a \u201cmodern\u201d cross-platform language? What was wrong with Objective-C anyway?<\/p>\n<p>Well, there&#8217;s a reason so many low-level frameworks are written in C++ or pure C: runtime speed. Objective-C&#8217;s dynamic dispatching has vastly improved over the years but is still a bottleneck, and in 95% of cases is not really necessary \u2014 we rarely use <code>id<\/code>, and strong typing is encouraged everywhere. As for pure C code, when you look at it, there&#8217;s always tons of crufty <code>#define<\/code>s, tricks to avoid C&#8217;s legacy problems, spinlocks and stack arrays and overflow checks and&#8230; so it&#8217;s no wonder Apple decided to start anew with a new language that avoids all of those problems and still interoperates with Cocoa etc. \u2014 all while the infrastructure&#8217;s being changed underneath.<\/p>\n<p>So, why not C++? Lattner is a C++ wizard, right? All of Clang\/LLVM is coded in C++. So is WebKit, Apple&#8217;s other major open-source success. I can&#8217;t see that changing, and their effort to fully support all of C++&#8217;s experimental future features argues that it won&#8217;t change. But C++ doesn&#8217;t look like a good match for internal Apple technologies like GCD and ARC, and\u00a0the C++ Standards Committee is certainly not interested in adopting those. On the other hand, judiciously adopting certain things like generics, operator overloading and optimized dispatching is certainly a good thing. And last but not least, Apple now owns\/controls the entire toolchain <em>and<\/em> the systems programming language!<\/p>\n<p>More later;\u00a0I&#8217;ve started to write an entire application in Swift and after that may feel qualified to comment on language details. For now, I&#8217;m quite happy with the prospects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over\u00a0two weeks ago, Apple at WWDC announced something entirely unexpected: thousands of new APIs and a brand-new programming language, Swift. No hardware, of course; it&#8217;s a developers conference, remember? Reactions varied all over the spectrum. Non-developers (especially \u201cindustry analysts\u201d) mostly had no idea what it meant: they said Apple had announced \u201cnothing\u201d. Almost all\u00a0developers, however, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3,4,19],"tags":[14,13,47,21,22],"class_list":["post-2865","post","type-post","status-publish","format-standard","hentry","category-apple","category-dev","category-software","tag-clang","tag-llvm","tag-swift","tag-wwdc","tag-xcode"],"featured_image_src":null,"author_info":{"display_name":"Rainer Brockerhoff","author_link":"https:\/\/brockerhoff.net\/blog\/author\/rbrockerhoff\/"},"jetpack_shortlink":"https:\/\/wp.me\/p1q3Zc-Kd","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/2865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/comments?post=2865"}],"version-history":[{"count":0,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/posts\/2865\/revisions"}],"wp:attachment":[{"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/media?parent=2865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/categories?post=2865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brockerhoff.net\/blog\/wp-json\/wp\/v2\/tags?post=2865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}