{"id":2201,"date":"2014-07-28T19:52:22","date_gmt":"2014-07-28T19:52:22","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2201"},"modified":"2014-07-28T19:52:22","modified_gmt":"2014-07-28T19:52:22","slug":"operator-overloading-in-f-2","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/operator-overloading-in-f-2\/","title":{"rendered":"Operator overloading in F#"},"content":{"rendered":"<p>More adventures with F#&#8230;<\/p>\n<p>Operator overloading is documented perfectly well in <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd233204.aspx\" title=\"Operator Overloading (F#)\" target=\"_blank\">Operator Overloading (F#)<\/a> but to summarize I&#8217;ve created this post&#8230;<\/p>\n<p>Operators in F# can be overloaded at the class, record type or global level. <\/p>\n<p><strong>Overloading an operator on a class or record type<\/strong><\/p>\n<p>Let&#8217;s look at the syntax for overloading an operator on a class or record type.<\/p>\n<p><em>Note: Reproduced from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd233204.aspx\" title=\"Operator Overloading (F#)\" target=\"_blank\">Operator Overloading (F#)<\/a> page<\/em><\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nstatic member (operator-symbols) (parameter-list&gt; =\r\n    method-body\r\n<\/pre>\n<p><strong>Overloading an operator which is globally accessible<\/strong><\/p>\n<p>Let&#8217;s look at the syntax for overloading an operator at a global level.<\/p>\n<p><em>Note: Reproduced from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd233204.aspx\" title=\"Operator Overloading (F#)\" target=\"_blank\">Operator Overloading (F#)<\/a> post<\/em><\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet &#x5B;inline] (operator-symbols) parameter-list = \r\n    function-body\r\n<\/pre>\n<p><strong>More&#8230;<\/strong><\/p>\n<p>You can overload the &#8220;standard&#8221; operators but you can also create your own operators. For example FAKE creates it&#8217;s own global operator to combine two file paths using the @@ operator<\/p>\n<p><em>The below is taken from the FAKE source code available in <a href=\"https:\/\/github.com\/fsharp\/FAKE\/blob\/master\/src\/app\/FakeLib\/EnvironmentHelper.fs\" title=\"EnvironmentHelper.fs\" target=\"_blank\">github<\/a><\/em><\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet inline (@@) path1 path2 = combinePaths path1 path2\r\n<\/pre>\n<p>As it&#8217;s pretty obvious, this is a global level overload of a newly created operator, the @@ which takes two parameters.<\/p>\n<p>Where an operator may be used for binary or unary (infix or prefix), such as + or &#8211; (+., -., &#038;, &#038;&#038;, %, and %% may also be used for infix or prefix operators) we need to prefix the operator with a tilde ~, hence to overload the + and use as a unary operator we&#8217;d write something like<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet inline (~+) x = &#x5B; x ]\r\n\r\n\/\/ in use we'd have\r\n\r\nlet v = + &quot;hello&quot;\r\n<\/pre>\n<p>Whereas using the + as a binary operator we&#8217;d write something like<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet inline (+) x y = &#x5B; x, y ]\r\n\r\n\/\/ in use we'd have\r\n\r\nlet v = &quot;hello&quot; + &quot;world&quot;\r\n<\/pre>\n<p>If you&#8217;re coming from C# to F# you&#8217;ll have already noticed the &#8220;funky&#8221; set of operators that F# supports. As already shown with the @@ operator sample (taken from FAKE) you can combine operators to produce all sorts of new operators, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet (&lt;==&gt;) x  y = &#x5B; x, y]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>More adventures with F#&#8230; Operator overloading is documented perfectly well in Operator Overloading (F#) but to summarize I&#8217;ve created this post&#8230; Operators in F# can be overloaded at the class, record type or global level. Overloading an operator on a class or record type Let&#8217;s look at the syntax for overloading an operator on a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-2201","post","type-post","status-publish","format-standard","hentry","category-f"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2201"}],"version-history":[{"count":12,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2201\/revisions"}],"predecessor-version":[{"id":2236,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2201\/revisions\/2236"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}