{"id":23773,"date":"2018-12-27T14:32:11","date_gmt":"2018-12-27T19:32:11","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=23773"},"modified":"2019-01-31T08:31:37","modified_gmt":"2019-01-31T13:31:37","slug":"dynamiccallable-unix-tools-as-swift-functions","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/12\/27\/dynamiccallable-unix-tools-as-swift-functions\/","title":{"rendered":"@dynamicCallable: Unix Tools as Swift Functions"},"content":{"rendered":"<p><a href=\"http:\/\/www.alwaysrightinstitute.com\/\/swift-dynamic-callable\/\">Helge He&szlig;<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.alwaysrightinstitute.com\/\/swift-dynamic-callable\/\">\n<p>A new feature in Swift 5 are\n<a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0216-dynamic-callable.md\">Dynamic Callable<\/a>&rsquo;s.\nWe combine this with the related\n<a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0195-dynamic-member-lookup.md\">Dynamic Member Lookup<\/a>\nfeature to expose the filesystem and Unix shell commands as\nregular Swift objects and functions.<\/p>\n<p>[&#8230;]<\/p><p>This is intended as a demo. It should work just fine, but in the name of error handling and proper Swift beauty, you might want to approach forking processes differently &#x1F913;<\/p><p>[&#8230;]<\/p><p>An obvious limitation is that both features are statically typed. You can&rsquo;t lookup one function thats returns an Int, and another function which returns a String. You have to tell the compiler in advance what type you expect.<\/p><p>[&#8230;]<\/p><p>Another limitation is that the reverse is not possible, i.e. you cannot \nlookup a Callable for a Swift function and dynamically invoke it via\n<code>m.dynamicallyCall(withArguments:)<\/code>.<\/p><\/blockquote>\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2018\/07\/23\/schema-less-database-with-dynamic-swift\/\">Schema-less Database With Dynamic Swift<\/a>, <a href=\"https:\/\/mjtsai.com\/blog\/2018\/06\/29\/exploring-dynamicmemberlookup\/\">Exploring @dynamicMemberLookup<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Helge He&szlig;: A new feature in Swift 5 are Dynamic Callable&rsquo;s. We combine this with the related Dynamic Member Lookup feature to expose the filesystem and Unix shell commands as regular Swift objects and functions. [&#8230;]This is intended as a demo. It should work just fine, but in the name of error handling and proper [&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-12-27T19:32:13Z","apple_news_api_id":"f532ad61-914d-4e36-b279-4a34df4838a9","apple_news_api_modified_at":"2019-01-31T13:31:41Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/A9TKtYZFNTjayeUo030g4qQ","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":[767,46,74,71,901],"class_list":["post-23773","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bash","tag-languagedesign","tag-opensource","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23773","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=23773"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23773\/revisions"}],"predecessor-version":[{"id":24124,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/23773\/revisions\/24124"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=23773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=23773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=23773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}