{"id":27221,"date":"2019-11-06T16:37:41","date_gmt":"2019-11-06T21:37:41","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=27221"},"modified":"2019-11-06T16:37:41","modified_gmt":"2019-11-06T21:37:41","slug":"static-types-in-swiftui","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/11\/06\/static-types-in-swiftui\/","title":{"rendered":"Static Types in SwiftUI"},"content":{"rendered":"<p><a href=\"https:\/\/www.objc.io\/blog\/2019\/11\/05\/static-types-in-swiftui\/\">Chris Eidhof<\/a> (<a href=\"https:\/\/twitter.com\/objcio\/status\/1191749676577513472\">tweet<\/a>, <a href=\"https:\/\/news.ycombinator.com\/item?id=21455941\">Hacker News<\/a>):<\/p>\n<blockquote cite=\"https:\/\/www.objc.io\/blog\/2019\/11\/05\/static-types-in-swiftui\/\">\n<p>In SwiftUI, the implementation works differently. In our <code>stack<\/code> above, SwiftUI knows the type: a vertical stack view with two subviews. During the execution of the program this type will never change &mdash;&nbsp;it&rsquo;s a static property of the code. As such, our program will always render a vertical stack view with a text and a rectangle. When the state changes, some of the views&rsquo; properties might change, but the stack view with the two subviews will always persist.<\/p>\n<p>This hard guarantee from the type system means that SwiftUI doesn&rsquo;t need to do a tree diff. Instead, it only needs to look at the properties of each view, and update those on screen. Theoretically, this still involves walking the entire tree, but walking a tree has a much lower complexity than diffing a tree.<\/p>\n<p>[&#8230;]<\/p>\n<p>For view trees that have a variable length, SwiftUI uses <code>ForEach<\/code>. We won&rsquo;t go into detail on <code>ForEach<\/code>, but SwiftUI requires you to provide either a constant range, or, if the length is truly dynamic, to use an identifier for each element you&rsquo;re displaying. When the elements change, <code>ForEach<\/code> uses the identifier to uniquely identify elements during a diffing step.<\/p>\n<p>[&#8230;]<\/p>\n<p>An <code>AnyView<\/code> is a type-erased view, and as such, it provides no information at compile-time about what&rsquo;s inside. SwiftUI will need to do more work at runtime to verify changes (as mentioned <a href=\"https:\/\/twitter.com\/mgorbach\/status\/1136154973635432448\">here<\/a>, and <a href=\"https:\/\/twitter.com\/ricketson_\/status\/1138891031233978368\">here<\/a>).<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Chris Eidhof (tweet, Hacker News): In SwiftUI, the implementation works differently. In our stack above, SwiftUI knows the type: a vertical stack view with two subviews. During the execution of the program this type will never change &mdash;&nbsp;it&rsquo;s a static property of the code. As such, our program will always render a vertical stack view [&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":"2019-11-06T21:37:44Z","apple_news_api_id":"b041319f-33f2-4a03-89c1-b06858dff063","apple_news_api_modified_at":"2019-11-06T21:37:45Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AsEExnzPySgOJwbBoWN_wYw","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,1667,30,1666,71,901,1812],"class_list":["post-27221","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-ios-13","tag-mac","tag-macos-10-15","tag-programming","tag-swift-programming-language","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27221","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=27221"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27221\/revisions"}],"predecessor-version":[{"id":27222,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27221\/revisions\/27222"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=27221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=27221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=27221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}