{"id":11805,"date":"2015-07-23T10:50:26","date_gmt":"2015-07-23T14:50:26","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11805"},"modified":"2015-07-24T11:14:45","modified_gmt":"2015-07-24T15:14:45","slug":"failable-init-vs-throws","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/07\/23\/failable-init-vs-throws\/","title":{"rendered":"init? vs. init throws"},"content":{"rendered":"<p><a href=\"http:\/\/owensd.io\/2015\/07\/07\/failable-init-vs-throws.html\">David Owens II<\/a>:<\/p>\n<blockquote cite=\"http:\/\/owensd.io\/2015\/07\/07\/failable-init-vs-throws.html\">\n<p>What I&rsquo;ve come to realize is, my problem is really about the callsite, I actually <em>always<\/em> want to use <code>throws<\/code>. The simple fact is that I can return more information about the failure and let the caller decide if they care about it or not.<\/p>\n<\/blockquote>\n<p>But if the caller doesn&rsquo;t care about it, this makes the callsite ugly.<\/p>\n<blockquote cite=\"http:\/\/owensd.io\/2015\/07\/07\/failable-init-vs-throws.html\">\n<p>Ok, so let&rsquo;s make it better:<\/p>\n<pre>func trythrow&lt;T&gt;(@autoclosure fn: () throws -&gt; T?) -&gt; T? {\n    do { return try fn() }\n    catch { return nil }\n}\n\nfunc example3() {\n    guard let tt = trythrow(try Foo(throwable: 0)) else {\n        print(\"error\")\n        return\n    }\n    \n    print(\"Foo(throwable:) creation passed\")\n    \n}<\/pre>\n<p>This gets us into a happy place again. We can turn failures where we do not care about the specific reasons into a code path that is more natural. If this becomes a pattern, it begs the question why this wouldn&rsquo;t be just built into the language.<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/forums.developer.apple.com\/thread\/8186\">This thread<\/a> suggests doing that by adding <code>try?<\/code>, which looks like a good idea.<\/p>","protected":false},"excerpt":{"rendered":"<p>David Owens II: What I&rsquo;ve come to realize is, my problem is really about the callsite, I actually always want to use throws. The simple fact is that I can return more information about the failure and let the caller decide if they care about it or not. But if the caller doesn&rsquo;t care about [&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":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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,46,30,71,901],"class_list":["post-11805","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-languagedesign","tag-mac","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11805","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=11805"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11805\/revisions"}],"predecessor-version":[{"id":11809,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11805\/revisions\/11809"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}