{"id":23091,"date":"2018-10-15T15:05:28","date_gmt":"2018-10-15T19:05:28","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=23091"},"modified":"2018-10-15T15:05:28","modified_gmt":"2018-10-15T19:05:28","slug":"building-dsls-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/10\/15\/building-dsls-in-swift\/","title":{"rendered":"Building DSLs in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.swiftbysundell.com\/posts\/building-dsls-in-swift\">John Sundell<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.swiftbysundell.com\/posts\/building-dsls-in-swift\">\n<p>So let&rsquo;s take things one step further, and enable our above code to be used as a proper DSL. The first thing we&rsquo;ll need is an execution context. One reason that DSLs can remove so much verbosity and cruft, is that they&rsquo;re used in a very specific context, that itself already provides much of the information required to understand what the code does.<\/p>\n<p>[&#8230;]<\/p>\n<p>For our context, we&rsquo;ll take some inspiration from the <code>UIView.animate<\/code> API, and use a closure to encapsulate the usage of our DSL. All we need to make that happen is a simple extension on <code>UIView<\/code> that adds a method that in turn calls our context closure.<\/p>\n<p>[&#8230;]<\/p>\n<p>Let&rsquo;s see how we can improve our DSL using operators - starting with overloading the plus and minus operators to enable us to combine a layout anchor and a constant into a tuple - which&rsquo;ll later let us act on them as one unit[&#8230;]<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>John Sundell: So let&rsquo;s take things one step further, and enable our above code to be used as a proper DSL. The first thing we&rsquo;ll need is an execution context. One reason that DSLs can remove so much verbosity and cruft, is that they&rsquo;re used in a very specific context, that itself already provides much [&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":"2018-10-15T19:05:32Z","apple_news_api_id":"88b315fe-9f3a-46ee-b4cf-50f84e5ae905","apple_news_api_modified_at":"2018-10-15T19:05:33Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AiLMV_p86Ru60z1D4TlrpBQ","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":[200,69,31,1610,30,1609,71,901],"class_list":["post-23091","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-autolayout","tag-cocoa","tag-ios","tag-ios-12","tag-mac","tag-macos-10-14","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23091","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=23091"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23091\/revisions"}],"predecessor-version":[{"id":23092,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23091\/revisions\/23092"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=23091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=23091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=23091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}