{"id":1133,"date":"2018-09-15T23:55:22","date_gmt":"2018-09-15T21:55:22","guid":{"rendered":"http:\/\/www.albertopasca.it\/whiletrue\/?p=1133"},"modified":"2018-08-10T22:24:08","modified_gmt":"2018-08-10T20:24:08","slug":"swift-custom-operators","status":"publish","type":"post","link":"https:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/","title":{"rendered":"Swift &#8211; Custom Operators"},"content":{"rendered":"<p>This is an argument that I like a lot, the possibility to create or to override an operator changing its functionality.<\/p>\n<p>SImilar to <strong>C++ Operator Overloading<\/strong> (<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/operators\" target=\"_blank\" rel=\"noopener\">https:\/\/en.cppreference.com\/w\/cpp\/language\/operators<\/a>) but in <strong>Swift<\/strong>.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2018\/08\/flatternOperator.png\" class=\"size-full\"><\/p>\n<p><!--more--><\/p>\n<p>Let&#8217;s see a simple example just to convince you to use it!<\/p>\n<h6>Percentage calculation<\/h6>\n<p>Why don&#8217;t you use the percentage sign &#8220;<strong>%<\/strong>&#8221; to calculate the percentage?<\/p>\n<p>What do you think of this:<\/p>\n<pre class=\"p1\"><span class=\"s1\">var<\/span> fivePercent = <strong><span class=\"s2\">5<\/span>%<\/strong>\u2029<span class=\"s3\">print<\/span><span class=\"s4\">( <\/span>fivePercent<span class=\"s4\"> ) \/\/\u00a0<\/span><span class=\"s1\">0.05<\/span><\/pre>\n<p>If you like it, you can continue reading!<\/p>\n<hr>\n<h2>TL;DR<\/h2>\n<p>How works this percentage sign?<\/p>\n<p>New operators are declared at a global level using the\u00a0<code><span class=\"pre\">operator<\/span><\/code>\u00a0keyword, and are marked with the\u00a0<code><span class=\"pre\">prefix<\/span><\/code>,\u00a0<code><span class=\"pre\">infix<\/span><\/code>\u00a0or\u00a0<code><span class=\"pre\">postfix<\/span><\/code>\u00a0modifiers:<\/p>\n<pre><span class=\"k\">prefix[postfix, infix]<\/span> <span class=\"k\">operator<\/span> %<\/pre>\n<p>This means that your new operator (%) can be used as:<\/p>\n<ul>\n<li><code><span class=\"pre\">postfix<\/span><\/code> like\u00a0<strong>5% <\/strong><\/li>\n<li><code><span class=\"pre\">prefix<\/span><\/code> like\u00a0<strong>%5<\/strong><\/li>\n<li><code><span class=\"pre\">infix<\/span><\/code> like\u00a0 <strong>2%5<\/strong><\/li>\n<\/ul>\n<p>as you prefer.<\/p>\n<p>Example for percentage using <strong>POSTFIX<\/strong>:<\/p>\n<pre class=\"p1\"><strong>postfix<\/strong> operator<strong><span class=\"s1\"> %<\/span><\/strong>\u2029<span class=\"s2\">postfix<\/span> <span class=\"s2\">func<\/span> <strong>%<\/strong> (percentage: <span class=\"s3\">Int<\/span>) -&gt; <span class=\"s3\">Double<\/span> {\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s2\">return<\/span> (<span class=\"s3\">Double<\/span>(percentage) \/ <span class=\"s4\">100<\/span>)\u2029}\u2029\u2029<span class=\"s2\">var<\/span> fivePercent = <strong><span class=\"s4\">5<\/span>%<\/strong>\u2029<span class=\"s5\">print<\/span><span class=\"s1\">( <\/span>fivePercent<span class=\"s1\"> )<\/span><\/pre>\n<p>that&#8217;s all. You can use now\u00a0<strong>5%<\/strong> and get <strong>0.05<\/strong> as value.<\/p>\n<p>Let&#8217;s see an example using <strong>PREFIX<\/strong>, the square root:<\/p>\n<pre class=\"p1\">prefix operator<strong><span class=\"s1\"> \u221a<\/span><\/strong>\u2029<span class=\"s2\">prefix<\/span> <span class=\"s2\">func<\/span> <strong>\u221a<\/strong>(lhs: <span class=\"s3\">Double<\/span>) -&gt; <span class=\"s3\">Double<\/span> {\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s2\">return<\/span> <span class=\"s4\">sqrt<\/span>(lhs)\u2029\u2029}\u2029\u2029<span class=\"s2\">let<\/span> someVal: <span class=\"s3\">Double<\/span> = <span class=\"s5\">25<\/span>\u2029<span class=\"s2\">let<\/span> squareRoot = <strong>\u221a<span class=\"s6\">someVal<\/span><\/strong>\u2029<span class=\"s4\">print<\/span><span class=\"s1\">( <\/span>squareRoot<span class=\"s1\"> )<\/span><\/pre>\n<p>Using operator prefix you can put your symbol before\u00a0your number or your variable.<\/p>\n<p>The last one is the <strong>INFIX<\/strong> operator that is used between numbers. Example:<\/p>\n<pre class=\"p1\">infix operator<strong><span class=\"s1\"> \u2022<\/span><\/strong>\u2029<span class=\"s2\">func<\/span> <strong>\u2022<\/strong>(lhs: <span class=\"s3\">Double<\/span>, rhs: <span class=\"s3\">Double<\/span>) -&gt; <span class=\"s3\">Double<\/span> {\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s2\">return<\/span> lhs * lhs + rhs * rhs\u2029}\u2029\u2029<span class=\"s2\">let<\/span> doubleVal1 = <span class=\"s4\">2.0<\/span>\u2029<span class=\"s2\">let<\/span> doubleVal2 = <span class=\"s4\">3.0<\/span>\u2029\u2029<span class=\"s2\">let<\/span><span class=\"s1\"> squareSum = <\/span><strong>doubleVal1<span class=\"s1\"> \u2022 <\/span>doubleVal2<\/strong>\u2029<span class=\"s5\">print<\/span><span class=\"s1\">( <\/span>\"squareSum: <span class=\"s1\">\\<\/span>(<span class=\"s6\">squareSum<\/span>)\"<span class=\"s1\"> ) \/\/<\/span><span class=\"s1\"><strong>squareSum: 13.0<\/strong><\/span><\/pre>\n<p>this operator can be used between numbers.<\/p>\n<p>Cool right?<\/p>\n<p>Now, something more interesting about operators. This was the funny part, now the complicated one.<\/p>\n<h6>precedencegroup<\/h6>\n<p>For every operator, we can define the <strong>precedence <\/strong>using the keyword <code><span class=\"pre\">precedenceGroup<\/span><\/code>.<\/p>\n<p>Read more also on Apple reference:\u00a0<a href=\"https:\/\/developer.apple.com\/documentation\/swift\/swift_standard_library\/operator_declarations\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.apple.com\/documentation\/swift\/swift_standard_library\/operator_declarations\u00a0<\/a><\/p>\n<pre class=\"p1\"><strong><span class=\"s1\">precedencegroup<\/span><\/strong> PowerPrecedence {\u2029<span class=\"s2\"><span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><\/span>associativity<span class=\"s2\">: right<\/span>\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">higherThan<\/span>: <strong>MultiplicationPrecedence<\/strong>\u2029}\u2029\u2029<span class=\"s1\">infix<\/span> <span class=\"s1\">operator<\/span> <strong>^^^<\/strong>: <strong>PowerPrecedence<\/strong>\u2029<span class=\"s1\">func<\/span> <strong>^^^<\/strong> (base: <span class=\"s3\">Int<\/span>, power: <span class=\"s3\">Int<\/span>) -&gt; <span class=\"s3\">Double<\/span> {\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s1\">return<\/span> <span class=\"s4\">pow<\/span>(<span class=\"s3\">Double<\/span>(base), <span class=\"s3\">Double<\/span>(power))\u2029}\u2029\u2029<span class=\"s1\">let<\/span> fiveSquared = <strong><span class=\"s5\">5<\/span> ^^^ <span class=\"s5\">2<\/span><\/strong>\u2029<span class=\"s4\">print<\/span><span class=\"s2\">( <\/span>fiveSquared<span class=\"s2\"> ) \/\/<\/span><span class=\"s1\">fiveSquared: 25.0<\/span><\/pre>\n<p>It&#8217;s nice, but is better to do using <strong>generics<\/strong>, no?<\/p>\n<p>You can find a recap here:<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"d5V2Be9DgD\"><p><a href=\"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/\">Swift &#8211; Use generics [T] with closures<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Swift &#8211; Use generics [T] with closures&#8221; &#8212; while (true) {\u00a0}\" src=\"https:\/\/www.albertopasca.it\/whiletrue\/swift-use-generics-with-closures\/embed\/#?secret=vG1IiztyvC#?secret=d5V2Be9DgD\" data-secret=\"d5V2Be9DgD\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<pre class=\"p1\">infix operator<span class=\"s1\"> ^^<\/span>\u2029<span class=\"s2\">public<\/span> <span class=\"s2\">func<\/span> ^^ <strong>&lt;T: <span class=\"s3\">Numeric<\/span>&gt;<\/strong>( value: <strong><span class=\"s2\">inout<\/span> <span class=\"s3\">T<\/span><\/strong>, power: <span class=\"s3\">Int<\/span>) {\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s2\">var<\/span> finalValue: <strong><span class=\"s3\">T<\/span><\/strong> = <span class=\"s4\">1<\/span>\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s2\">for<\/span> <span class=\"s2\">_<\/span> <span class=\"s2\">in<\/span> <span class=\"s4\">0<\/span>..&lt;power {\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>finalValue = finalValue * value\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>}\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>value = finalValue\u2029}<\/pre>\n<p>This operator, ^^ execute the &#8220;pow&#8221; function but don&#8217;t care of the\u00a0type of the value.<\/p>\n<p>So you can &#8220;<strong>pow<\/strong>&#8221; any type you want:<\/p>\n<pre class=\"p1\"><span class=\"s1\">var<\/span> floatValue: <span class=\"s2\">Float<\/span> <span class=\"Apple-converted-space\">\u00a0 <\/span>= <span class=\"s3\">28.10<\/span>\u2029<span class=\"s1\">var<\/span> doubleValue: <span class=\"s2\">Double<\/span> = <span class=\"s3\">10.84<\/span>\u2029<span class=\"s1\">var<\/span> intValue: <span class=\"s2\">Int<\/span> <span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 <\/span>= <span class=\"s3\">123<\/span>\u2029<span class=\"s1\">var<\/span> binaryValue <span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span>= <span class=\"s3\">0b101<\/span>\u2029<span class=\"s1\">var<\/span> octalValue<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span>= <span class=\"s3\">0o11<\/span>\u2029<span class=\"s1\">var<\/span> hexadecimalValue<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>= <span class=\"s3\">0xFA<\/span>\u2029\u2029<strong>floatValue<span class=\"s1\"> ^^ <\/span>2<\/strong>\u2029<span class=\"s2\">print<\/span><span class=\"s1\">( <\/span>floatValue<span class=\"s1\"> ) \/\/<\/span><span class=\"s1\">789.61005<\/span>\u2029\u2029<strong><span class=\"s1\">hexadecimalValue ^^ 2<\/span><\/strong>\u2029<span class=\"s1\">print(hexadecimalValue ) \/\/<\/span><span class=\"s1\">62500<\/span><\/pre>\n<p>So, valid for any type of value!<\/p>\n<hr>\n<p>Another interesting thing is about the structures like <strong>CGRect<\/strong>, <strong>CGPoint<\/strong>, and others&#8230;<\/p>\n<p>You can create an operator, for instance, to add a &#8220;delta&#8221; to the <strong>x<\/strong>, <strong>y<\/strong>, <strong>w<\/strong>, <strong>h<\/strong> of a <strong>CGRect<\/strong> (don&#8217;t know the utility but..) or why not to sum two different <strong>CGRect<\/strong>:<\/p>\n<pre class=\"p1\">infix operator<span class=\"s1\"> ++<\/span>\u2029extension <span class=\"s2\">CGRect<\/span><span class=\"s1\"> {<\/span>\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s3\">static<\/span> <span class=\"s3\">func<\/span> +(lhs: <span class=\"s2\">CGRect<\/span>, rhs: <span class=\"s2\">CGFloat<\/span>) -&gt; <span class=\"s2\">CGRect<\/span> {\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span class=\"s3\">return<\/span> <span class=\"s2\">CGRect<\/span>(x: lhs.<span class=\"s4\">origin<\/span>.<span class=\"s4\">x<\/span> + rhs,\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span>y: lhs.<span class=\"s4\">origin<\/span>.<span class=\"s4\">y<\/span> + rhs,\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span>width: lhs.<span class=\"s4\">size<\/span>.<span class=\"s4\">width<\/span> + rhs,\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span>height: lhs.<span class=\"s4\">size<\/span>.<span class=\"s4\">height<\/span> + rhs)\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>}\u2029\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s3\">static<\/span> <span class=\"s3\">func<\/span> ++(lhs: <span class=\"s2\">CGRect<\/span>, rhs: <span class=\"s2\">CGRect<\/span>) -&gt; <span class=\"s2\">CGRect<\/span> {\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 <\/span><span class=\"s3\">return<\/span> <span class=\"s2\">CGRect<\/span>(x: lhs.<span class=\"s4\">origin<\/span>.<span class=\"s4\">x<\/span> + rhs.<span class=\"s4\">origin<\/span>.<span class=\"s4\">x<\/span>,\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span>y: lhs.<span class=\"s4\">origin<\/span>.<span class=\"s4\">y<\/span> + rhs.<span class=\"s4\">origin<\/span>.<span class=\"s4\">y<\/span>,\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span>width: lhs.<span class=\"s4\">size<\/span>.<span class=\"s4\">width<\/span> + rhs.<span class=\"s4\">size<\/span>.<span class=\"s4\">width<\/span>,\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <\/span>height: lhs.<span class=\"s4\">size<\/span>.<span class=\"s4\">height<\/span> + rhs.<span class=\"s4\">size<\/span>.<span class=\"s4\">height<\/span>)\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span>}\u2029}<\/pre>\n<p>To avoid operator caos in your code, add it in an extension like this example.<\/p>\n<pre class=\"p1\"><span class=\"s1\">let<\/span> frame1 = <span class=\"s2\">CGRect<\/span>(x: <span class=\"s3\">10<\/span>, y: <span class=\"s3\">10<\/span>, width: <span class=\"s3\">100<\/span>, height: <span class=\"s3\">100<\/span>)\u2029<span class=\"s1\">let<\/span> biggerFrame = <span class=\"s4\">frame1<\/span> + <span class=\"s3\">10<\/span>\u2029<span class=\"s1\">print<\/span><span class=\"s2\">( <\/span>\"biggerFrame: <span class=\"s2\">\\<\/span>(<span class=\"s3\">biggerFrame<\/span>)\"<span class=\"s2\"> )<\/span>\u2029\u2029<span class=\"s1\">let<\/span> frame2 = <span class=\"s2\">CGRect<\/span>(x: <span class=\"s3\">30<\/span>, y: <span class=\"s3\">30<\/span>, width: <span class=\"s3\">300<\/span>, height: <span class=\"s3\">300<\/span>)\u2029<span class=\"s1\">let<\/span> frameSum = <span class=\"s4\">frame1<\/span> ++ <span class=\"s4\">frame2<\/span>\u2029<span class=\"s1\">print<\/span><span class=\"s2\">( <\/span>\"frameSum: <span class=\"s2\">\\<\/span>(<span class=\"s3\">frameSum<\/span>)\"<span class=\"s2\"> )<\/span><\/pre>\n<p>Result is:<\/p>\n<pre class=\"p1\"><span class=\"s1\">biggerFrame: (20.0, 20.0, 110.0, 110.0)<\/span>\u2029<span class=\"s1\">frameSum: (40.0, 40.0, 400.0, 400.0)<\/span><\/pre>\n<p>You can create more and more based on your needed but&#8230;<\/p>\n<hr>\n<h6 id=\"should-you-use-operators-1358\">Should You Use Operators?<\/h6>\n<p>As suggested in a talk by <em><a href=\"https:\/\/academy.realm.io\/posts\/slug-erica-sadun-operators-strong-opinions\/\" target=\"_blank\" rel=\"noopener\">Erica Sadun<\/a><\/em>, I report some notes about using operator and why and when to use, but also when to avoid!<\/p>\n<p><strong>Main points:<\/strong><\/p>\n<ul>\n<li id=\"operators-lack-context\"><em>Operators lack context<\/em><\/li>\n<\/ul>\n<blockquote><p>Operators, especially custom ones, naturally lack the context and cues that allow you to relate information and functionality to things that you are seeing in code as you look at it.<\/p>\n<\/blockquote>\n<ul>\n<li id=\"operators-lack-context\"><em>Association<\/em><\/li>\n<\/ul>\n<blockquote><p>Association helps you to narrow down concepts by relating terms to an idea. Most of all, it assists recall. In Xcode, an associated word feeds into its auto-completion system in a way that pure symbols can\u2019t.<\/p>\n<\/blockquote>\n<ul>\n<li id=\"unnatural-operators\"><em>Unnatural operators<\/em><\/li>\n<\/ul>\n<blockquote><p>Unnatural operators aren\u2019t naturally recognizable. Put yourself into the shoes of someone reading your code and not the person who wrote the operator.<\/p>\n<\/blockquote>\n<ul>\n<li id=\"someone-will-read-your-code\"><em>Someone will read your code<\/em><\/li>\n<\/ul>\n<blockquote><p>The compiler isn\u2019t the only one who\u2019s ever going to read your code. You are going to read your code as you write it, and you\u2019re going to read it in a month or a year from now.<\/p>\n<\/blockquote>\n<p><strong>And important lessons<\/strong>:<\/p>\n<blockquote><p>Operators are <strong>precious and expensive<\/strong>. They involve significant mental costs for creating code for <strong>reading code<\/strong>, and <strong>maintaining code<\/strong>. Use operators sparingly.<\/p>\n<p>A great operator that\u2019s <strong>impossible to type<\/strong> isn\u2019t going to get used.<\/p>\n<p>Think global. First, prefer <strong>symbols that can be as easily entered<\/strong> on the German keyboard as a US keyboard.<\/p>\n<p>When you adopt an operator <strong>use it, use it<\/strong>! If you\u2019re not using an operator significantly more than you would a function, seriously consider cutting it out and just replace it with a function.<\/p>\n<\/blockquote>\n<h6>BONUS OPERATOR: &#8220;THE Flatter operator&#8221;<\/h6>\n<p>Given an array with optional values, return an array with only NOT nil values.<\/p>\n<pre class=\"p1\">postfix operator<strong><span class=\"s1\"> .?<\/span><\/strong>\u2029<span class=\"s2\">postfix<\/span> <span class=\"s2\">func<\/span> <strong>.?<\/strong>&lt;T&gt;(lhs: <span class=\"s3\">Array<\/span>&lt;<span class=\"s3\">T<\/span>?&gt;) -&gt; <span class=\"s3\">Array<\/span>&lt;<span class=\"s3\">T<\/span>&gt; {\u2029<span class=\"Apple-converted-space\">\u00a0 \u00a0 <\/span><span class=\"s2\">return<\/span> lhs.<strong><span class=\"s4\">flatMap<\/span><\/strong>{ $0 }\u2029}\u2029\u2029<span class=\"s2\">let<\/span> numbers:[<span class=\"s3\">Int<\/span>?] = [<span class=\"s5\">1<\/span>, <strong><span class=\"s2\">nil<\/span><\/strong>, <span class=\"s5\">2<\/span>, <span class=\"s5\">3<\/span>, <strong><span class=\"s2\">nil<\/span><\/strong>, <span class=\"s5\">6<\/span>]\u2029<span class=\"s4\">print<\/span><span class=\"s1\">( <\/span><strong>numbers<span class=\"s1\">.?<\/span><\/strong><span class=\"s1\"> ) \/\/<\/span><span class=\"s1\">[1, 2, 3, 6]<\/span><\/pre>\n<p>My favourite!<\/p>\n<p>Enjoy, but don&#8217;t forget the suggestions!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is an argument that I like a lot, the possibility to create or to override an operator changing its functionality. SImilar to C++ Operator Overloading (https:\/\/en.cppreference.com\/w\/cpp\/language\/operators) but in Swift.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","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_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[218,729,632,730,731,732,648,727,720,719,721,726,724,728,725,723,689,722,690],"class_list":["post-1133","post","type-post","status-publish","format-standard","hentry","category-generic","tag-c","tag-cgpoint","tag-cgrect","tag-erica-sadun","tag-flat-map","tag-flatmap","tag-generics","tag-infix","tag-operator","tag-overloading","tag-percentage","tag-postfix","tag-pow","tag-precedence","tag-prefix","tag-radix","tag-realm","tag-square","tag-struct"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Swift - Custom Operators - while (true) {\u00a0}<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift - Custom Operators - while (true) {\u00a0}\" \/>\n<meta property=\"og:description\" content=\"This is an argument that I like a lot, the possibility to create or to override an operator changing its functionality. SImilar to C++ Operator Overloading (https:\/\/en.cppreference.com\/w\/cpp\/language\/operators) but in Swift.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/\" \/>\n<meta property=\"og:site_name\" content=\"while (true) {\u00a0}\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-15T21:55:22+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2018\/08\/flatternOperator.png\" \/>\n<meta name=\"author\" content=\"Alberto Pasca\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@lupasca\" \/>\n<meta name=\"twitter:site\" content=\"@lupasca\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alberto Pasca\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/#article\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/\"},\"author\":{\"name\":\"Alberto Pasca\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"headline\":\"Swift &#8211; Custom Operators\",\"datePublished\":\"2018-09-15T21:55:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/\"},\"wordCount\":651,\"publisher\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"image\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/flatternOperator.png\",\"keywords\":[\"c++\",\"cgpoint\",\"CGRect\",\"erica sadun\",\"flat map\",\"flatmap\",\"generics\",\"infix\",\"operator\",\"overloading\",\"percentage\",\"postfix\",\"pow\",\"precedence\",\"prefix\",\"radix\",\"realm\",\"square\",\"struct\"],\"articleSection\":[\"generic\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/\",\"url\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/\",\"name\":\"Swift - Custom Operators - while (true) {\u00a0}\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/#primaryimage\"},\"image\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/flatternOperator.png\",\"datePublished\":\"2018-09-15T21:55:22+00:00\",\"breadcrumb\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/flatternOperator.png?fit=952%2C356&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/flatternOperator.png?fit=952%2C356&ssl=1\",\"width\":952,\"height\":356},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-custom-operators\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift &#8211; Custom Operators\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\",\"url\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\",\"name\":\"while (true) {\u00a0}\",\"description\":\"notes from a developer, tutorials, how-to, ideas\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\",\"name\":\"Alberto Pasca\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"caption\":\"Alberto Pasca\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\"},\"description\":\"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.\",\"sameAs\":[\"https:\\\/\\\/www.albertopasca.it\",\"albertopasca\",\"https:\\\/\\\/x.com\\\/lupasca\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift - Custom Operators - while (true) {\u00a0}","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/","og_locale":"en_US","og_type":"article","og_title":"Swift - Custom Operators - while (true) {\u00a0}","og_description":"This is an argument that I like a lot, the possibility to create or to override an operator changing its functionality. SImilar to C++ Operator Overloading (https:\/\/en.cppreference.com\/w\/cpp\/language\/operators) but in Swift.","og_url":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/","og_site_name":"while (true) {\u00a0}","article_published_time":"2018-09-15T21:55:22+00:00","og_image":[{"url":"http:\/\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2018\/08\/flatternOperator.png","type":"","width":"","height":""}],"author":"Alberto Pasca","twitter_card":"summary_large_image","twitter_creator":"@lupasca","twitter_site":"@lupasca","twitter_misc":{"Written by":"Alberto Pasca","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/#article","isPartOf":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/"},"author":{"name":"Alberto Pasca","@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"headline":"Swift &#8211; Custom Operators","datePublished":"2018-09-15T21:55:22+00:00","mainEntityOfPage":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/"},"wordCount":651,"publisher":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"image":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/#primaryimage"},"thumbnailUrl":"http:\/\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2018\/08\/flatternOperator.png","keywords":["c++","cgpoint","CGRect","erica sadun","flat map","flatmap","generics","infix","operator","overloading","percentage","postfix","pow","precedence","prefix","radix","realm","square","struct"],"articleSection":["generic"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/","url":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/","name":"Swift - Custom Operators - while (true) {\u00a0}","isPartOf":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/#primaryimage"},"image":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/#primaryimage"},"thumbnailUrl":"http:\/\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2018\/08\/flatternOperator.png","datePublished":"2018-09-15T21:55:22+00:00","breadcrumb":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/#primaryimage","url":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2018\/08\/flatternOperator.png?fit=952%2C356&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2018\/08\/flatternOperator.png?fit=952%2C356&ssl=1","width":952,"height":356},{"@type":"BreadcrumbList","@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-custom-operators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.albertopasca.it\/whiletrue\/"},{"@type":"ListItem","position":2,"name":"Swift &#8211; Custom Operators"}]},{"@type":"WebSite","@id":"https:\/\/www.albertopasca.it\/whiletrue\/#website","url":"https:\/\/www.albertopasca.it\/whiletrue\/","name":"while (true) {\u00a0}","description":"notes from a developer, tutorials, how-to, ideas","publisher":{"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.albertopasca.it\/whiletrue\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac","name":"Alberto Pasca","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","caption":"Alberto Pasca"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g"},"description":"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.","sameAs":["https:\/\/www.albertopasca.it","albertopasca","https:\/\/x.com\/lupasca"]}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/1133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/comments?post=1133"}],"version-history":[{"count":9,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/1133\/revisions"}],"predecessor-version":[{"id":1161,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/1133\/revisions\/1161"}],"wp:attachment":[{"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/media?parent=1133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/categories?post=1133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/tags?post=1133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}