12 ms·
Swift 4.0 Released
- dep_b 9y agoConverting two existing projects was pretty painless. Though the larger and older of the two had a significant amount of automatically generated changes. Most of them were (Void) in blocks changed to () though. Another nice extra is mixing 3.2 with 4.0 libraries.
- afro88 9y agoThe changes to tuples in closures were a little annoying too, with the jump to 4.0. But on a whole it was pretty painless for us too.
- swivelmaster 9y ago"Swift 4 includes a faster, easier to use String implementation that retains Unicode correctness and adds support for creating, using and managing substrings." This alone is grounds for opening and drinking very expensive champagne and/or wine.
- jhancock 9y agoAs a new swift'er working on an app with my 12 year old son, I reasonate with your comment. We were near the end of our first app and hit a need in swift 3 to figure out locale/language settings so needed to manipulate strings. I was shocked at how unobvious it seemed. Fortunately, I found a swift playground cheat sheet and left it to my son to explore. A couple hours later he had it worked out ;)
- abalone 9y agovar wine = Set<String>() wine.insert("Champagne")
- willtim 9y agoSo strings are mutable? I'm not sure that's a reason to celebrate.
- slavapestov 9y agoThe example does not demonstrate mutability of strings; it is constructing a set of strings and inserting a string element. Only the set is being mutated. But yes, in Swift, arrays, dictionaries, sets and strings are all mutable. They are also value types, so mutation is a purely local effect.
- xrisk 9y agoWould that not make passing around data expensive? Or are you expected to pass some kind of pointer instead?
- slavapestov 9y agoCopy-on-write alleviates some of the overhead. Copies are only performed where required to preserve value semantics.
- mantas 9y agoAll of these can be both mutable or immutable. And converted as needed let array = [1, 2, 3] var mutableArray = [4, 5, 6] var anotherMutable = array Not sure how much optimisation is done under the hood, but Swift compiler loves to complain about mutable structures that are not ever edited.
- masklinn 9y agoThese are all copy-on-write boxed types with value semantics: the value you pass around is a pointer (with some additional data) and Swift has mechanisms/API which let the implementor copy the backing buffer on mutation if necessary: https://developer.apple.com/documentation/swift/2429905-isknownuniquelyreferenced https://developer.apple.com/documentation/swift/2429905-iskn...
- fauigerzigerk 9y ago
- dep_b 9y agoOof, magic strings. Always use enums! enum Booze: String { case wine, case champagne, case beer, case whiskey } let wine = [Booze.champagne] </ocd>
- golergka 9y agoI feel like adding a pull request to add whisky to the list. Some of us prefer scotch.
- ryan-koch 9y agoDefinitely have to +1 this. I'd be happy to write some tests to get scotch on board.
- koolba 9y agoI'm not sure if it was intentional but "case beer" made me smile.
- meggar 9y agowine's type should conform to a Stack protocol so you can pop champagne.
- rimliu 9y agoWhy are these String, not just enums?
- dep_b 9y agoBecause it's replacing a Magic String?
- timjver 9y agoYou don't need commas :)
- abalone 9y agoNooooooo... you're right about enums but this codifies the same error I was trying to elucidate with code. Here, let's try this: enum Booze { case Wine(region: String) case Beer case Whiskey } let drink = Booze.Wine(region: "Champagne") Now.. Anyone want to demonstrate how to use enums for the wine region or, say, whiskey subtypes? Should we use classes?
- developer2 9y ago>> support for creating, using and managing substrings Can I finally extract a substring from the middle of a string using integer offsets, in one line? Something like string.utf8.substr(3, 5)? Or does it still take an absolutely insane and unacceptable 4 lines of code with throwaway temp variables to track the start and end indexes? The inability to do basic string manipulations is a major turnoff, to the point it makes the entire language look like a bad joke.
- fauigerzigerk 9y agoYou could do something like String(s.dropFirst(3).prefix(5))
- slavapestov 9y agoA String in Swift is a sequence of graphemes, unlike most languages which model strings as sequences of code points or just rawutf8, so there's no way to do efficient O(1) indexing. This is why the API is a bit more complex than you'd like.
- developer2 9y agoI keep getting this inaccurate answer of "they're just grapheme code points - you can't do it". That is why my example specifically used the .utf8 view. It's already possible to do exactly what I'm talking about, but it takes 4 complex lines of code, needing two calls to index() with startIndex/endIndex references and offsetBy arguments. The existing solution to this problem already uses integers (the offsetBy arguments)! I'm not talking about refactoring the String type, or breaking backwards compatibility. There would be no change in how the underlying String storage or access works. It's about adding a level of (zero overhead!) abstraction to simplify a common, basic operation. Every other language I've ever touched has easy to use substrings. It makes absolutely no sense that Swift requires assigning throwaway constants to represent the start and end indexes for a simple substring extraction. There's this level of superiority regarding the purity and correctness of Swift's String type that clouds developers' ability to see that there is no limitation to doing this. It blows my mind that the most modern major language is this clunky.
- mixmastamyk 9y agoUbuntu 16.10 is already out of support, they should update that. Artful is out in a month or so.
- hasenj 9y agoThis is great news. However, I wish the mac release was available as independent binaries without having to come along with Xcode.
- OberstKrueger 9y agoThe Command Line Tools include the Swift compiler without needing Xcode installed. Open up a terminal and type “swift” to enter the REPL or “swiftc” to compile a Swift File. SPM is included too. If they’re not already installed, running one of those commands will prompt you to install them.
- hasenj 9y agoIf I already have swift-3 and Xcode 8, how do I install only swift-4?
- gre 9y agoYou can download a toolchain snapshot from here. There's a swift-4.0-branch download if you scroll a bit. https://swift.org/download/#snapshots https://swift.org/download/#snapshots
- slavapestov 9y agoKeep in mind that binaries built with the toolchain snapshots cannot be submitted to the App Store. You need an Xcode release for that.
- deleted 9y ago[deleted]
- Scarbutt 9y agoI received a CLT update today, it brought swift 4 with it.
- mmariani 9y agoDo you have homebrew installed? If so... brew install swiftenv
- valine 9y agoThe new JSON parser is really slick. https://developer.apple.com/documentation/swift/codable https://developer.apple.com/documentation/swift/codable
- hasenj 9y agoAm I the only one who thinks it's weird that Codable is a magic interface? Are there any other "magic" interfaces?
- alexashka 9y agoIt's not that weird - it's simply that Swift currently has no meta-programming features, in the interest of focusing on many other aspects of the language. As a result, Codable had to be baked directly into the language, or uses private reflection APIs not available to the public. Objective-C does have meta-programming features [0] and that's how all the really nifty features were made possible (KVO, CoreData, etc) What Swift is aiming for, is to be a performant, general purpose programming language. This restrains it in the amount of clever stuff you can do. That and the inter-op requirement has been an incredible hurdle when it comes to focusing on features that people actually want out of Swift. A lot of effort has gone into being able to use Swift with existing Obj-C libraries - I sincerely hope they'll at some point say 'no more interop, only Swift, and by the way we've rewritten the UI libraries to use generics, so that you can do more than cute generic demos in your code.' [0] https://genius.com/Soroush-khanlou-metaprogramming-isnt-a-scary-word-not-even-in-objective-c-annotated https://genius.com/Soroush-khanlou-metaprogramming-isnt-a-sc...
- sinhpham 9y agoThey implemented what looks like the Rust ownership model: SE-0176 Enforce Exclusive Access to Memory (https://github.com/apple/swift-evolution/blob/master/proposals/0176-enforce-exclusive-access-to-memory.md https://github.com/apple/swift-evolution/blob/master/proposa...), but I'm having a hard time understanding the proposal, can anyone shed some light on this?
- slavapestov 9y agoYou can think of inout parameters in Swift as something analogous to a mutable borrow in Rust. Until Swift 4 we allowed overlapping inout access, for example: var counter = 0 func foo(x: inout Int) { x += 1 print(counter) } foo(x: &counter) Note how 'counter' is read by 'foo(x:)' during an inout access of the same value. This is now prohibited by Swift 4, using a combination of static and dynamic checks. This fixes some undefined behavior and will also enable more aggressive compiler optimizations to be added in the future.
- hellofunk 9y ago> Note how 'counter' is read by 'foo(x:)' during an inout access of the same value It's not clear to me in your example why reading the value of counter after mutating it is bad; why is this now prohibited?
- mikeash 9y agoSwift specifies inout parameters as copying the value that's passed in, giving the copy to the callee as a mutable value, and then writing back the value to the original storage after the function returns. & is not an "address of" operator. Of course, it would be inefficient to do this all the time, so Swift will optimize copy-then-writeback to just passing a pointer to the original storage whenever it can. But this is an optimization operating under the "as if" rule: as long as it works as if it does a copy-then-writeback, the compiler can make it actually do whatever it wants. If the example code were legal, then it would have to print `0`, because the writeback to `counter` doesn't happen until the end of the function. That means the compiler couldn't just pass in a pointer to `counter`, but would have to actually go through the copy-then-writeback procedure it's supposed to do, so you'd lose out on optimizations. Instead, Swift makes it illegal. You can't access a value while this call is happening. That allows the language semantics to coexist with optimizations.
- ramenmeal 9y agoI see some mentions of Server API's on their website. With these libraries in place, how comparable is swift to golang?
- hasenj 9y agoI think it's nicer to program in swift than go. It's already possible to do so to, by the way. http://perfect.org/ http://perfect.org/ What I think go has over swift is the complete independence from platform. You can write go code on osx and compile it to a linux binary and it Just Works! Swift on Linux is not perfect yet, though it can be made to work with some effort.
- alexashka 9y agoSwift has no quality high-level concurrency primitives - until those arrive, Swift is a complete non-starter as a server language. Concurrency is at the level of proposals currently, so it'll be another couple of years until they come up with something, and another couple until good server libraries/frameworks figure out how to use them properly. It's a shame really - I imagine this could've been moving along at a faster rate had Chris Lattner stayed on with Apple and recruited some extra help. He has written a really nice proposal for concurrency, that'll definitely influence Swift going forward. [0] [0] https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9f782 https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9...
- efficax 9y agoI agree, for the most part, but then again, C also has no high-level concurrency primitives, and basically every server ever is written in C. You can use Grand Central Dispatch, and it works. It's not as wonderful as go routines and channels, but hey, it works.
- alexashka 9y agoI hear you. C has performance because it doesn't have to do any sanity checks and because 20 years of serious effort has gone towards optimizing everything possible. The very motivation behind creating Swift, has been to have performance, and none of the undefined behaviour (cause of innumerable security breaches and unintentional complexity in compiler design and everyday programming) [0] To me, grand-central-dispatch feels one small step above semaphores/locks/monitors. There has been a lot of research in distributed computing and concurrency - I'd like to see libraries, language constructs and runtimes based on research fresher than before I was born :) [0] http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
- jorgemf 9y ago> Some source migration will be needed for many projects, although the number of source changes are quite modest compared to many previous major changes between Swift releases. It seems like the language still needs some time to be completely mature, or am I wrong? I haven't used it since 1.0
- slavapestov 9y agoThe pace of breaking changes is slowing down. Swift 3.1 was intended to be source compatible with 3.0, and Swift 4 has a "Swift 3.2" mode which tries to be source compatible with 3.0/3.1.
- jorgemf 9y agoSo, you agree with my comment? It still needs a bit more time to stabilize.
- kobeya 9y agoLanguage stability may not be a goal.
- jorgemf 9y agoThat is a problem for medium-big projects.
- valuearb 9y agoNot really. Xcode will automatically translate swift code to update it to a new version, and it has worked pretty well in most cases. The exception being the v2 to v3 transition, which was pretty onerous. My experience with some medium projects is that it costs me a morning every update. I'm okay with that because Swift is such a more productive and higher quality language than Objective C.
- 9y ago
- msie 9y agoWhy couldn't they make a PDF version of The Swift Programming Language? I hate ePub.
- nerdwaller 9y agoI'm not sure how well the conversion will capture links and such, but you can use Calibre[0] to do that conversion on all the major platforms, iirc (at least as a workaround). [0] https://calibre-ebook.com https://calibre-ebook.com
- msie 9y agoBeing voted down is nonsense. 1) I've tried ePub and it just doesn't work as well as PDF on my iPad mini, which is perfect for reading documents. 2) Virtually everyone sells their books in both PDF and ePub formats so I don't see why they can't do the same here.
- mmariani 9y agoI've bought an iPad Mini specifically to read ePubs. What do you think is broken on iBooks?
- Ensorceled 9y agoYour original comment is being voted down for simply "hating" ePub without providing any reason. Your second comment is being voted down because ePub works just fine in iBooks and on iPads for many of us, again, you need to provide details.
- dvfjsdhgfv 9y agoNote that in spite of supporting Ubuntu only, you can run the 4.0 version in Debian Stretch without any problems. Just download the package, unpack it and add the bin/ subdirectory to your $PATH. Edit: REPL stil doesn't allow importing Glibc by default, you need to specify the include path manually (e.g. swift -I /opt/swift-4.0-RELEASE-ubuntu16.10/usr/lib/swift/clang/include/).
- amelius 9y agoIgnorant question: What other language is Swift like most? And what improvements does Swift make over that language?
- catawbasam 9y agoKotlin is close
- jernfrost 9y agoKotlin, Rust, Scala and vaguely Go. Swift benefit over Kotlin is that it easily interfaces with existing objective-c and C code. It also has deterministic garbage collection through automatic reference counting which is better for interactive systems and maintaining low memory footprint. Since it is native code and doesn't have a complicated garbage collector it can be used like C to create libraries for other programming languages. This is impossible to do with Go and Kotlin. Compared to Rust it is considerably easier to learn. Go is probably easier to learn than Swift but Swift is a lot more expressive. You could do scientific computations in Swift but that would be cumbersome in Go since you can't easily define matrix, vector and point types yourself.
- favorited 9y agoPeople are posting comparisons to Kotlin, but for the record here's Chris Lattner's take on the Swift/Kotlin comparison: --- Swift and Kotlin evolved at about the same point in time with the same contemporary languages around them. And so the surface-level syntax does look very similar. … But if you go one level down below the syntax, the semantics are quite different. Kotlin is very reference semantics, it’s a thin layer on top of Java, and so it perpetuates through a lot of the Javaisms in its model. If we had done an analog to that for Objective-C it would be like, everything is an NSObject and it’s objc_msgSend everywhere, just with parentheses instead of square brackets. And a lot of people would have been happy with that for sure, but that wouldn’t have gotten us the functional features, that wouldn’t have gotten us value semantics, that wouldn’t have gotten us a lot of the safety things that are happening [in Swift]. I think that Kotlin is a great language. I really mean that. Kotlin is a great language, and they’re doing great things. They’re just under a different set of constraints. ---
- geodel 9y agoWith this yearly release cycle and amount of investment Apple is making, I think Swift 6 or 7 could become general purpose language for client/server code on non-apple platforms too.
- chhum 9y agoThere's a good summary of this release here: https://www.infoq.com/news/2017/09/swift-4-official-release https://www.infoq.com/news/2017/09/swift-4-official-release