{"id":18542,"date":"2017-08-03T16:27:46","date_gmt":"2017-08-03T20:27:46","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18542"},"modified":"2020-04-21T13:58:12","modified_gmt":"2020-04-21T17:58:12","slug":"required-swift-initializers-and-decodable","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/08\/03\/required-swift-initializers-and-decodable\/","title":{"rendered":"&ldquo;required&rdquo; Swift Initializers and Decodable"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/why-you-cant-make-someone-elses-class-decodable-a-long-winded-explanation-of-required-initializers\/6437\">Jordan Rose<\/a> (via <a href=\"https:\/\/swiftweekly.github.io\/issue-81\/\">Jesse Squires<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/why-you-cant-make-someone-elses-class-decodable-a-long-winded-explanation-of-required-initializers\/6437\"><p>The initializer is <code>required<\/code>, right? So all subclasses need to have access to it. But the implementation we provided here might not make sense for all subclasses&mdash;what if <code>VerySpecialSubclassOfPoint<\/code> doesn&rsquo;t have an <code>init(x:y:)<\/code> initializer? Normally, the compiler checks for this situation and makes the subclass reimplement the <code>required<\/code> initializer&#8230;but that only works if the <code>required<\/code> initializers are all known up front. So it can&rsquo;t allow this new <code>required<\/code> initializer to go by, because someone might try to call it dynamically on a subclass.<\/p>\n<p>[&#8230;]<\/p>\n<p><code>required<\/code> initializers are like methods: they may require dynamic dispatch. That means that they get an entry in the class&rsquo;s dynamic dispatch table, commonly known as its vtable. Unlike Objective-C method tables, vtables aren&rsquo;t set up to have entries arbitrarily added at run time<\/p>\n<p>[&#8230;]<\/p>\n<p>In most cases you don&rsquo;t care about hypothetical subclasses or invoking <code>init(from:)<\/code> on some dynamic <code>Point<\/code> type. If there was a way to mark <code>init(from:)<\/code> as something that was always available on subclasses, but dynamically checked to see if it was okay, we&rsquo;d be good.<\/p>\n<p>[&#8230;]<\/p>\n<p>For more information on this I suggest checking out my write-up of some of <a href=\"https:\/\/github.com\/apple\/swift\/blob\/master\/docs\/InitializerProblems.rst\">our initialization model problems<\/a>.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Jordan Rose (via Jesse Squires): The initializer is required, right? So all subclasses need to have access to it. But the implementation we provided here might not make sense for all subclasses&mdash;what if VerySpecialSubclassOfPoint doesn&rsquo;t have an init(x:y:) initializer? Normally, the compiler checks for this situation and makes the subclass reimplement the required initializer&#8230;but that [&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":"2020-04-21T17:58:15Z","apple_news_api_id":"fc623ee8-4c08-4c83-bc22-f95b707155ae","apple_news_api_modified_at":"2020-04-21T17:58:15Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/A_GI-6EwITIO8IvlbcHFVrg","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-18542","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\/18542","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=18542"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18542\/revisions"}],"predecessor-version":[{"id":28731,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18542\/revisions\/28731"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}