{"id":16946,"date":"2017-01-13T13:57:27","date_gmt":"2017-01-13T18:57:27","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16946"},"modified":"2020-04-21T13:49:20","modified_gmt":"2020-04-21T17:49:20","slug":"swift-3-protocols","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/01\/13\/swift-3-protocols\/","title":{"rendered":"Swift 3 Protocols"},"content":{"rendered":"<p><a href=\"http:\/\/inessential.com\/2016\/12\/30\/why_i_prefer_protocol-oriented-programmi\">Brent Simmons<\/a>:<\/p>\n<blockquote cite=\"http:\/\/inessential.com\/2016\/12\/30\/why_i_prefer_protocol-oriented-programmi\">\n<p>I first learned protocol-oriented-programming with Objective-C, and I was very pleased to see the Swift team emphasize this style.<\/p>\n<p>But, at least at this writing at the end of 2016, I still run into problems when I use this style of programming in Swift.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/forums.swift.org\/t\/pitch-add-the-defaultconstructible-protocol-to-the-standard-library\/4771\/9\">Dave Abrahams<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/pitch-add-the-defaultconstructible-protocol-to-the-standard-library\/4771\/9\"><p>Protocols (a.k.a. concepts) are not just bags of syntax; unless you can attach semantics to the operations, you can&rsquo;t write useful generic algorithms against them. So we shouldn&rsquo;t have <code>DefaultConstructible<\/code> for the same reason we shouldn&rsquo;t have &ldquo;Plusable&rdquo; to represent something that lets you write <code>x + x<\/code>.<\/p><\/blockquote>\n\n<p>Via <a href=\"https:\/\/oleb.net\/blog\/2016\/12\/protocols-have-semantics\/\">Ole Begemann<\/a> (<a href=\"https:\/\/twitter.com\/olebegemann\/status\/814942722570289152\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2016\/12\/protocols-have-semantics\/\"><p>Yet, by conforming a type to <code>Equatable<\/code> you also guarantee that your implementation follows the protocol&rsquo;s <em>semantics<\/em>, which are listed <a href=\"http:\/\/swiftdoc.org\/v3.0\/protocol\/Equatable\/\">in its documentation<\/a>.<\/p>\n<p>[&#8230;]<\/p>\n<p>One problem with a generic <code>init()<\/code> requirement without additional context is that <code>T()<\/code> means very different things for different <code>T<\/code>[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>The fourth argument against the <code>DefaultConstructible<\/code> protocol is that it clashes with Swift&rsquo;s policy to not initialize values to &ldquo;zero&rdquo; or some other default. In contrast to many other languages, Swift doesn&rsquo;t zero out memory for variables &mdash; the compiler forces the programmer to initialize every variable with an explicit value.<\/p><\/blockquote>\n\n<p><a href=\"http:\/\/www.figure.ink\/blog\/2016\/12\/31\/generic-programming\">Joshua Emmons<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.figure.ink\/blog\/2016\/12\/31\/generic-programming\"><p>I&rsquo;ve found these two papers particularly enlightening:<\/p><ul><li><a href=\"http:\/\/stepanovpapers.com\/genprog.pdf\">Generic Programming<\/a><\/li><li><a href=\"http:\/\/stepanovpapers.com\/DeSt98.pdf\">Fundamentals of Generic Programming<\/a><\/li><\/ul><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Brent Simmons: I first learned protocol-oriented-programming with Objective-C, and I was very pleased to see the Swift team emphasize this style. But, at least at this writing at the end of 2016, I still run into problems when I use this style of programming in Swift. Dave Abrahams: Protocols (a.k.a. concepts) are not just bags [&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-03-13T19:21:05Z","apple_news_api_id":"ba27374b-f62a-4384-bc41-d32fd63dd156","apple_news_api_modified_at":"2020-04-21T17:49:24Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/Auic3S_YqQ4S8QdMv1j3RVg","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":[46,71,901],"class_list":["post-16946","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16946","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=16946"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16946\/revisions"}],"predecessor-version":[{"id":28722,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16946\/revisions\/28722"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}