{"id":142,"date":"2020-05-20T15:46:17","date_gmt":"2020-05-20T12:46:17","guid":{"rendered":"http:\/\/www.juustila.com\/antti\/?p=142"},"modified":"2020-05-20T15:47:33","modified_gmt":"2020-05-20T12:47:33","slug":"fun-with-swift-operator-overloading","status":"publish","type":"post","link":"https:\/\/www.juustila.com\/antti\/2020\/05\/20\/fun-with-swift-operator-overloading\/","title":{"rendered":"Fun with Swift operator overloading"},"content":{"rendered":"<p>There is this guy in one forum, writing all his sentences ending with at least two periods, mostly three&#8230; Always&#8230; This might help him, integrated into some text editor&#8230; Saving his keyboard strokes&#8230;<\/p>\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #202020; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #d0d0d0\">extension<\/span> <span style=\"color: #24909d\">String<\/span> <span style=\"color: #d0d0d0\">{<\/span>\n   <span style=\"color: #6ab825; font-weight: bold\">static<\/span> <span style=\"color: #d0d0d0\">postfix<\/span> <span style=\"color: #d0d0d0\">func<\/span> <span style=\"color: #d0d0d0\">...<\/span> <span style=\"color: #d0d0d0\">(str:<\/span> <span style=\"color: #d0d0d0\">inout<\/span> <span style=\"color: #24909d\">String<\/span><span style=\"color: #d0d0d0\">)<\/span> <span style=\"color: #d0d0d0\">-&gt;<\/span> <span style=\"color: #24909d\">String<\/span> <span style=\"color: #d0d0d0\">{<\/span>\n      <span style=\"color: #d0d0d0\">str<\/span> <span style=\"color: #d0d0d0\">=<\/span> <span style=\"color: #24909d\">String<\/span><span style=\"color: #d0d0d0\">(str.map<\/span> <span style=\"color: #d0d0d0\">{<\/span>\n         <span style=\"color: #d0d0d0\">$0<\/span> <span style=\"color: #d0d0d0\">==<\/span> <span style=\"color: #ed9d13\">\".\"<\/span> <span style=\"color: #d0d0d0\">?<\/span> <span style=\"color: #ed9d13\">\"\u2026\"<\/span> <span style=\"color: #d0d0d0\">:<\/span> <span style=\"color: #d0d0d0\">$0<\/span>\n      <span style=\"color: #d0d0d0\">})<\/span>\n      <span style=\"color: #6ab825; font-weight: bold\">return<\/span> <span style=\"color: #d0d0d0\">str<\/span>\n   <span style=\"color: #d0d0d0\">}<\/span>\n<span style=\"color: #d0d0d0\">}<\/span>\n\n<span style=\"color: #6ab825; font-weight: bold\">var<\/span> <span style=\"color: #d0d0d0\">sentence<\/span> <span style=\"color: #d0d0d0\">=<\/span> <span style=\"color: #ed9d13\">\"Operator dotdotdot. For those. Who want to write. All sentences. With several periods.\"<\/span>\n<span style=\"color: #6ab825; font-weight: bold\">let<\/span> <span style=\"color: #d0d0d0\">hisStyle<\/span> <span style=\"color: #d0d0d0\">=<\/span> <span style=\"color: #d0d0d0\">sentence...<\/span>\n<span style=\"color: #d0d0d0\">print(hisStyle)<\/span>\n<span style=\"color: #999999; font-style: italic\">\/\/ Prints: \"Operator dotdotdot\u2026 For those\u2026 Who want to write\u2026 All sentences\u2026 With several periods\u2026\"<\/span>\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">What about adding a copyright at the end of text the user enters, using this brand new copyright operator, &lt;\u00a9><\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #202020; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #d0d0d0\">postfix<\/span> <span style=\"color: #d0d0d0\">operator<\/span> <span style=\"color: #d0d0d0\">&lt;<\/span><span style=\"color: #6ab825; background-color: #202020\">\u00a9<\/span><span style=\"color: #d0d0d0\">&gt;<\/span>\n<span style=\"color: #d0d0d0\">extension<\/span> <span style=\"color: #24909d\">String<\/span> <span style=\"color: #d0d0d0\">{<\/span>\n   <span style=\"color: #6ab825; font-weight: bold\">static<\/span> <span style=\"color: #d0d0d0\">postfix<\/span> <span style=\"color: #d0d0d0\">func<\/span> <span style=\"color: #d0d0d0\">&lt;<\/span><span style=\"color: #6ab825; background-color: #202020\">\u00a9<\/span><span style=\"color: #d0d0d0\">&gt;<\/span> <span style=\"color: #d0d0d0\">(str:<\/span> <span style=\"color: #d0d0d0\">inout<\/span> <span style=\"color: #24909d\">String<\/span><span style=\"color: #d0d0d0\">)<\/span> <span style=\"color: #d0d0d0\">-&gt;<\/span> <span style=\"color: #24909d\">String<\/span> <span style=\"color: #d0d0d0\">{<\/span>\n      <span style=\"color: #d0d0d0\">str<\/span> <span style=\"color: #d0d0d0\">+=<\/span> <span style=\"color: #ed9d13\">\" \u00a9 Antti J.\"<\/span>\n      <span style=\"color: #6ab825; font-weight: bold\">return<\/span> <span style=\"color: #d0d0d0\">str<\/span>\n   <span style=\"color: #d0d0d0\">}<\/span>\n<span style=\"color: #d0d0d0\">}<\/span>\n\n<span style=\"color: #6ab825; font-weight: bold\">var<\/span> <span style=\"color: #d0d0d0\">originalText<\/span> <span style=\"color: #d0d0d0\">=<\/span> <span style=\"color: #ed9d13\">\"Life is a b***h and then you die.\"<\/span>\n<span style=\"color: #6ab825; font-weight: bold\">var<\/span> <span style=\"color: #d0d0d0\">copyrighted<\/span> <span style=\"color: #d0d0d0\">=<\/span> <span style=\"color: #d0d0d0\">originalText&lt;<\/span><span style=\"color: #6ab825; background-color: #202020\">\u00a9<\/span><span style=\"color: #d0d0d0\">&gt;<\/span>\n<span style=\"color: #d0d0d0\">print(copyrighted)<\/span>\n<span style=\"color: #999999; font-style: italic\">\/\/ Prints: \"Life is a b***h and then you die. \u00a9 Antti J.\"<\/span>\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Well, back to some more serious work.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is this guy in one forum, writing all his sentences ending with at least two periods, mostly three&#8230; Always&#8230; This might help him, integrated into some text editor&#8230; Saving his keyboard strokes&#8230; extension String { static postfix func &#8230; (str: inout String) -&gt; String { str = String(str.map { $0 == &#8220;.&#8221; ? &#8220;\u2026&#8221; &hellip; <a href=\"https:\/\/www.juustila.com\/antti\/2020\/05\/20\/fun-with-swift-operator-overloading\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fun with Swift operator overloading&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[53,52,46],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-coding","tag-fun","tag-operator-overloading","tag-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":4,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/142\/revisions\/147"}],"wp:attachment":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}