{"id":26652,"date":"2019-09-23T16:45:26","date_gmt":"2019-09-23T20:45:26","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=26652"},"modified":"2019-09-24T16:33:40","modified_gmt":"2019-09-24T20:33:40","slug":"swift-5-1-released","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/09\/23\/swift-5-1-released\/","title":{"rendered":"Swift 5.1 Released"},"content":{"rendered":"<p><a href=\"https:\/\/swift.org\/blog\/swift-5-1-released\/\">Ted Kremenek<\/a>:<\/p>\n<blockquote cite=\"https:\/\/swift.org\/blog\/swift-5-1-released\/\">\n<p>Swift 5.1 builds on the strengths of Swift 5 by extending the stable features of the language to compile time with the introduction of module stability. With module stability it&rsquo;s now possible to create and share binary frameworks that will work with future releases of Swift. Swift 5.1 also extends the capabilities of the language and the standard library with new features such as property wrappers, opaque result types, key path member lookup, diffing for appropriate collection types, and new APIs for String. Altogether the new features of Swift 5.1 make it easier to design better APIs and reduce the amount of common boilerplate code.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/www.swiftbysundell.com\/articles\/5-small-but-significant-improvements-in-swift-5-1\/\">John Sundell<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.swiftbysundell.com\/articles\/5-small-but-significant-improvements-in-swift-5-1\/\">\n<p>While that&rsquo;s still possible, the scope of <code>Self<\/code> has now been extended to also include concrete types &mdash; like enums, structs and classes &mdash; enabling us to use <code>Self<\/code> as a sort of alias referring to a method or property&rsquo;s <em>enclosing<\/em> type, like this[&#8230;]<\/p>\n<\/blockquote>\n\n<p>Note that this is just syntactic sugar. It&rsquo;s not like <code>instancetype<\/code> in Objective-C.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/08\/28\/identifiable-in-swift-5-1\/\">Identifiable in Swift 5.1<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/04\/01\/whats-new-in-swift-5-1\/\">What&rsquo;s New in Swift 5.1<\/a><\/li>\n<\/ul>\n\n<p id=\"swift-5-1-released-update-2019-09-24\">Update (2019-09-24): As <a href=\"https:\/\/mjtsai.com\/blog\/2019\/09\/23\/swift-5-1-released\/#comment-3071047\">Joe Groff<\/a> notes, I misinterpreted Sundell&rsquo;s comments about <code>Self<\/code>. It doesn&rsquo;t refer to the <em>lexically<\/em> enclosing type, but rather to the dynamic type. So, when used in an expression, it&rsquo;s like <code>type(of: self)<\/code>:<\/p>\n\n<pre>class Base {\n    class func printClass() {\n        print(\"Base\")\n    }\n    \n    func selfPrintClass() {\n        Self.printClass()\n    }\n}\nclass Derived: Base {\n    override class func printClass() {\n        print(\"Derived\")\n    }\n}\nDerived().selfPrintClass() \/\/ prints Derived<\/pre>\n\n<p>This will be useful when <a href=\"https:\/\/mjtsai.com\/blog\/2017\/08\/31\/using-lazy-variables-to-work-around-swift-initialization-rules\/\">calling helper functions from initializers<\/a>.<\/p>\n\n<p>His comment that it&rsquo;s &ldquo;purely syntactic sugar&rdquo; only applies to this use of <code>Self<\/code>. When used to specify the return type, <code>Self<\/code> is not a mere substitution of the current class name. Instead, it refers to the type of the object (not the type that the method was defined on). So it&rsquo;s just like <code>instanceof<\/code> in Objective-C. And you can similarly use it to declare factory methods, in which case Swift will require you to implement them in terms of <code>required<\/code> initializers:<\/p>\n<pre>class Base {\n    required init() {} \/\/ must be provided unless Base is final\n\n    class func make() -&gt; Self {\n        return Self.init()\n    }\n}\nclass Derived: Base {}\nBase.make() \/\/ makes a Base\nDerived.make() \/\/ makes a Derived\n<\/pre>\n\n<p>So, <code>Self<\/code> is really cool, and it can now be used in many more places than before. It&rsquo;s still not allowed as a parameter type, though. I have <a href=\"https:\/\/mjtsai.com\/blog\/2018\/02\/20\/when-swift-makes-you-use-throws-instead-of-rethrows\/\">a case<\/a> where I want to ensure that a method has a parameter of the receiver&rsquo;s own type. I wasn&rsquo;t able to do this with <code>Self<\/code>, but I found a workaround using a protocol with an associated type.<\/p>","protected":false},"excerpt":{"rendered":"<p>Ted Kremenek: Swift 5.1 builds on the strengths of Swift 5 by extending the stable features of the language to compile time with the introduction of module stability. With module stability it&rsquo;s now possible to create and share binary frameworks that will work with future releases of Swift. Swift 5.1 also extends the capabilities of [&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":"2019-09-23T20:45:29Z","apple_news_api_id":"68ccf505-0212-4a41-8e34-34eea9356e15","apple_news_api_modified_at":"2019-09-24T20:33:46Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAw==","apple_news_api_share_url":"https:\/\/apple.news\/AaMz1BQISSkGONDTuqTVuFQ","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":[4],"tags":[31,1667,46,30,1666,71,901],"class_list":["post-26652","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-13","tag-languagedesign","tag-mac","tag-macos-10-15","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26652","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=26652"}],"version-history":[{"count":5,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26652\/revisions"}],"predecessor-version":[{"id":26662,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/26652\/revisions\/26662"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=26652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=26652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=26652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}