{"id":21725,"date":"2018-06-07T14:58:50","date_gmt":"2018-06-07T18:58:50","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=21725"},"modified":"2018-09-03T15:17:06","modified_gmt":"2018-09-03T19:17:06","slug":"swift-4-2","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/06\/07\/swift-4-2\/","title":{"rendered":"Swift 4.2"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/olebegemann\/status\/1004458683428950018\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/olebegemann\/status\/1004458683428950018\">\n<p>I made a &ldquo;What&rsquo;s new in Swift 4.2&rdquo; <a href=\"https:\/\/github.com\/ole\/whats-new-in-swift-4-2\">playground<\/a> for Xcode 10: new collection algorithms, random numbers, enumerating enum cases, and more. <\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/oleb.net\/blog\/2018\/06\/random-numbers-in-swift\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2018\/06\/random-numbers-in-swift\/\">\n<p>Working with random numbers in Swift used to be a bit of pain because there was no native random number API. Developers had to fall back on C APIs provided by the operating system, which are often easy to misuse. Moreover, the preferred random number API differs between platforms (e.g. <a href=\"https:\/\/man.openbsd.org\/arc4random.3\"><code>arc4random<\/code><\/a> vs. <a href=\"http:\/\/en.cppreference.com\/w\/c\/numeric\/random\/rand\"><code>rand<\/code><\/a> vs. <a href=\"http:\/\/man7.org\/linux\/man-pages\/man3\/random.3.html\"><code>random<\/code><\/a>), making it difficult to write cross-platform code.<\/p>\n\n<p>Swift 4.2 makes this much easier by including a native and fairly full-featured random number API in the standard library. You can read about the full API and its design rationale in <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0202-random-unification.md\" title=\"Swift Evolution proposal SE-0202: Random Unification\">Swift Evolution proposal SE-0202<\/a>).<\/p>\n<\/blockquote>\n\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2018\/04\/01\/swift-4-1\/\">Swift 4.1<\/a>.<\/p>\n\n<p>Update (2018-06-09): <a href=\"http:\/\/roadfiresoftware.com\/2018\/06\/wwdc-2018-whats-new-in-swift-recap\/\">Roadfire Software<\/a> (via <a href=\"https:\/\/news.ycombinator.com\/item?id=17264936\">Hacker News<\/a>):<\/p>\n<blockquote cite=\"http:\/\/roadfiresoftware.com\/2018\/06\/wwdc-2018-whats-new-in-swift-recap\/\"><p>In What&rsquo;s New in Swift at WWDC 2018, Apple gave a quick overview of what&rsquo;s new in Swift 4.2 and Swift 5. You can read my notes below, or you can <a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2018\/401\/\">watch the 40-minute video<\/a> and download the slides from Apple.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/olebegemann\/status\/1005166437110075392\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/olebegemann\/status\/1005166437110075392\">\n<p>Problem: a manual CaseIterable implementation risks becoming incorrect as a type evolves. The compiler doesn&rsquo;t catch this.<\/p>\n<p>Solution: a dummy function whose only purpose is to produce a compile error on (almost) the correct line when a new case is added.<\/p>\n<p>It&rsquo;s ugly but it works.<\/p>\n<\/blockquote>\n\n<p>Update (2018-06-12): <a href=\"https:\/\/oleb.net\/blog\/2018\/06\/enumerating-enum-cases\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2018\/06\/enumerating-enum-cases\/\">\n<p>New in Swift 4.2, the compiler can generate a collection of an enum&rsquo;s cases, relieving you from the error-prone task of maintaining such a list yourself. The Swift Evolution proposal that introduced this feature is <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0194-derived-collection-of-enum-cases.md\" title=\"Swift Evolution proposal SE-0194: Derived Collection of Enum Cases\">SE-0194<\/a>.<\/p>\n<p>[&#8230;]<\/p>\n<p>The manual implementation is easy enough to write, but it has a major downside compared to compiler-generated code: we now have to remember to keep it up to date. If we later add another workout type to our enum, the compiler won&rsquo;t alert us that our <code>allCases<\/code> implementation is no longer correct. It will just silently return the wrong result.<\/p>\n\n<p>The only workaround I can think of is a little ugly, but it solves this problem in a really interesting way.<\/p>\n<\/blockquote>\n\n<p>Update (2018-08-14): <a href=\"https:\/\/nshipster.com\/hashable\/\">Mattt Thompson<\/a>:<\/p>\n<blockquote cite=\"https:\/\/nshipster.com\/hashable\/\"><p>Swift 4.2 refines <code>Hashable<\/code> even further by introducing the <code>Hasher<\/code> type and adopting a new universal hashing function.<\/p><\/blockquote>\n\n<p id=\"swift-4-2-update-2018-09-03\">Update (2018-09-03): <a href=\"http:\/\/inessential.com\/2018\/09\/03\/netnewswire_diary_1_automatic_hashing_an\">Brent Simmons<\/a>:<\/p>\n<blockquote cite=\"http:\/\/inessential.com\/2018\/09\/03\/netnewswire_diary_1_automatic_hashing_an\">\n<p>The point still stands, though, that automatic hashing in the case of objects with lots of properties might be a performance hit. As always &mdash;&nbsp;use the profiler.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: I made a &ldquo;What&rsquo;s new in Swift 4.2&rdquo; playground for Xcode 10: new collection algorithms, random numbers, enumerating enum cases, and more. Ole Begemann: Working with random numbers in Swift used to be a bit of pain because there was no native random number API. Developers had to fall back on C APIs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2018-06-12T18:33:43Z","apple_news_api_id":"c9dcb48d-5f38-42e0-9115-6d7fa2635ab7","apple_news_api_modified_at":"2018-09-03T19:17:11Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAw==","apple_news_api_share_url":"https:\/\/apple.news\/Aydy0jV84QuCRFW1_omNatw","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[],"tags":[46,71,813,901],"class_list":["post-21725","post","type-post","status-publish","format-standard","hentry","tag-languagedesign","tag-programming","tag-random-numbers","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/21725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=21725"}],"version-history":[{"count":5,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/21725\/revisions"}],"predecessor-version":[{"id":22601,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/21725\/revisions\/22601"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=21725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=21725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=21725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}