{"id":42006,"date":"2024-02-05T14:34:00","date_gmt":"2024-02-05T19:34:00","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=42006"},"modified":"2024-02-05T14:34:00","modified_gmt":"2024-02-05T19:34:00","slug":"t-and-optionalt-are-not-the-same-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/02\/05\/t-and-optionalt-are-not-the-same-in-swift\/","title":{"rendered":"T? and Optional&lt;T&gt; Are Not the Same in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/mastodon.social\/@nicklockwood\/111879035400480882\">Nick Lockwood<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@nicklockwood\/111879035400480882\">\n<p>I once again got bitten by the fact that <code>T?<\/code> properties in a Swift struct become optional parameters in the synthesized initializer, making it easy to accidentally omit a property that you meant to set.<\/p>\n<p>The workaround is either to use <code>let<\/code> instead of <code>var<\/code>, or to use <code>Optional&lt;T&gt;<\/code> instead of <code>T?<\/code><\/p>\n<\/blockquote>\n\n<p>Swift has lots of syntactic sugar, which usually makes writing it simpler and easier, but sometimes multiple bits of sugar interact or obscure what&rsquo;s going on.<\/p>\n\n<p><a href=\"https:\/\/mastodon.social\/@jjoelson\/111879090330148517\">Jonathan Joelson<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@jjoelson\/111879090330148517\">\n<p>The fact that something is nullable does not remotely imply that null is a sensible default.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Nick Lockwood: I once again got bitten by the fact that T? properties in a Swift struct become optional parameters in the synthesized initializer, making it easy to accidentally omit a property that you meant to set. The workaround is either to use let instead of var, or to use Optional&lt;T&gt; instead of T? Swift [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2024-02-05T19:34:02Z","apple_news_api_id":"b0224e25-98c1-4f07-a4ce-67ca8ad4f82a","apple_news_api_modified_at":"2024-02-05T19:34:02Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AsCJOJZjBTwekzmfKitT4Kg","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-42006","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\/42006","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=42006"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/42006\/revisions"}],"predecessor-version":[{"id":42007,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/42006\/revisions\/42007"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=42006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=42006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=42006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}