{"id":9775,"date":"2022-11-24T21:00:58","date_gmt":"2022-11-24T21:00:58","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9775"},"modified":"2022-11-24T21:00:58","modified_gmt":"2022-11-24T21:00:58","slug":"swifts-discardableresult","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/swifts-discardableresult\/","title":{"rendered":"Swift&#8217;s @discardableResult"},"content":{"rendered":"<p>When we&#8217;re writing Swift code we might have a function that returns a value as part of its invocation. For example, maybe we&#8217;ve a function which sends some data to a server and then returns a boolean to denote success or failure, here&#8217;s a mock up<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfunc serviceCall() -&gt; Bool {\r\n    \/\/ do something real here\r\n    true\r\n}\r\n\r\nWe call this method but maybe we don't care what the result is, so we simply call it like this\r\n\r\n&#x5B;code]\r\nserviceCall()\r\n<\/pre>\n<p>If we build this, swift will report <em>warning: result of call to &#8216;serviceCall()&#8217; is unused serviceCall()<\/em>. We can get around this using a discard, for example<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nlet _ = serviceCall()\r\n\/\/ OR\r\n_ = serviceCall()\r\n<\/pre>\n<p>but there&#8217;s another way to tell the compiler to ignore this warning. We mark the function with the <em>@discardableResult<\/em> attribute, i.e. <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n@discardableResult\r\nfunc serviceCall() -&gt; Bool {\r\n    \/\/ do something real here\r\n    true\r\n}\r\n<\/pre>\n<p>Admittedly this seems a little odd, that the function being called should say, &#8220;hey you can just ignore the result&#8221;. This is something that should probably be used with care, although if it&#8217;s known that the function&#8217;s result is likely to be ignored, then it&#8217;s better than having lots of discards all over the place to ignore the result of the function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we&#8217;re writing Swift code we might have a function that returns a value as part of its invocation. For example, maybe we&#8217;ve a function which sends some data to a server and then returns a boolean to denote success or failure, here&#8217;s a mock up func serviceCall() -&gt; Bool { \/\/ do something real [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9775","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9775"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9775\/revisions"}],"predecessor-version":[{"id":9779,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9775\/revisions\/9779"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}