{"id":1012,"date":"2014-01-13T20:54:00","date_gmt":"2014-01-13T20:54:00","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1012"},"modified":"2014-01-13T20:54:00","modified_gmt":"2014-01-13T20:54:00","slug":"operator-overloading-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/operator-overloading-in-f\/","title":{"rendered":"Operator overloading in F#"},"content":{"rendered":"<p>F# allows us to override existing operators and even create our own. There are two types of operators, prefix and infix.<\/p>\n<p>Prefix operators are those which take one operand. Infix operators take two arguments.Unlike C# both operands in an infix operator must be of the same type.<\/p>\n<p>Along with overloading existing operators we can create custom operators using a single or multiple characters from the following <strong>!$%&#038;+-,\/<=>?@^|~<\/strong><\/p>\n<p>For example, let&#8217;s define a global operator !^ which will calculate the square root of a floating point number<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet inline (!^) (a: float) = Math.Sqrt(a)\r\n<\/pre>\n<p>and in usage<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet sqrt = !^ 36.0\r\nprintfn &quot;Sqrt %f&quot; sqrt\r\n<\/pre>\n<p>An infix operator is declared taking two arguments, so here&#8217;s an example of a value to the power n, which we&#8217;ll use the operator ++ which can be defined as<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet inline (++) (x: float) (y: float) = Math.Pow(x, y)\r\n<\/pre>\n<p>and in usage<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet a = 10.0 ++ 2.0\r\n<\/pre>\n<p>When declaring operators on a type (as opposed to globally) we have the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\ntype MyClass2(x : int) =\r\n    member this.value = x\r\n    static member (~+) (m : MyClass2) = \r\n        MyClass2(m.value + 1)\r\n    static member (+) (m : MyClass2, v : int) = \r\n        MyClass2(m.value + v)\r\n\r\n\/\/ usage \r\n\r\nlet m = new MyClass2(1)\r\nlet n = m + 5\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>F# allows us to override existing operators and even create our own. There are two types of operators, prefix and infix. Prefix operators are those which take one operand. Infix operators take two arguments.Unlike C# both operands in an infix operator must be of the same type. Along with overloading existing operators we can create [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1012","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\/1012","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=1012"}],"version-history":[{"count":9,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1012\/revisions"}],"predecessor-version":[{"id":1066,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1012\/revisions\/1066"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}