{"id":12698,"date":"2015-11-02T14:54:48","date_gmt":"2015-11-02T19:54:48","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12698"},"modified":"2015-11-02T14:56:10","modified_gmt":"2015-11-02T19:56:10","slug":"if-let-assignment-operator","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/11\/02\/if-let-assignment-operator\/","title":{"rendered":"If-Let Assignment Operator"},"content":{"rendered":"<p><a href=\"http:\/\/www.alloc-init.com\/2015\/10\/if-let-assignment\/\">Weston Hanners<\/a> defines a custom Swift operator (via <a href=\"https:\/\/swiftnews.curated.co\/issues\/60\">This Week in Swift<\/a>):<\/p>\n<blockquote cite=\"http:\/\/www.alloc-init.com\/2015\/10\/if-let-assignment\/\">\n<pre>infix operator ?= { associativity right precedence 90 }\n\nfunc ?=&lt;T&gt;(inout left: T, right: T?) {\n&nbsp; &nbsp; if let value = right {\n&nbsp; &nbsp; &nbsp; &nbsp; left = value\n&nbsp; &nbsp; }\n}\n\nfunc ?=&lt;T&gt;(inout left: T?, right: T?) {\n&nbsp; &nbsp; if let value = right {\n&nbsp; &nbsp; &nbsp; &nbsp; left = value\n&nbsp; &nbsp; }\n}<\/pre>\n<\/blockquote>\n<p>So that code like:<\/p>\n<pre>if let value = someOptionalValue as? String {\n  self.value = value\n}\n<\/pre>\n<p>Can be written as:<\/p>\n<pre>self.value ?= someOptionalValue as? String<\/pre>\n<p>I like the brevity, but often I care that there was no value, or perhaps want to report why there wasn&rsquo;t one (e.g. which key was missing). So I kind of see this as a way to make the compiler happy, which is not necessarily the same thing as properly handling the unexpected situation. I might prefer a pattern where the helper extracts the value from the containing object, rather than checking it after the fact.<\/p>","protected":false},"excerpt":{"rendered":"<p>Weston Hanners defines a custom Swift operator (via This Week in Swift): infix operator ?= { associativity right precedence 90 } func ?=&lt;T&gt;(inout left: T, right: T?) { &nbsp; &nbsp; if let value = right { &nbsp; &nbsp; &nbsp; &nbsp; left = value &nbsp; &nbsp; } } func ?=&lt;T&gt;(inout left: T?, right: T?) { &nbsp; [&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":[71,901],"class_list":["post-12698","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12698","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=12698"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12698\/revisions"}],"predecessor-version":[{"id":12706,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12698\/revisions\/12706"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}