{"id":2720,"date":"2014-12-12T21:41:10","date_gmt":"2014-12-12T21:41:10","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2720"},"modified":"2014-12-12T21:41:10","modified_gmt":"2014-12-12T21:41:10","slug":"function-overloading-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/function-overloading-in-f\/","title":{"rendered":"Function overloading in F#"},"content":{"rendered":"<p>F# doesn&#8217;t support function overloads, so you cannot write the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet add a b = a + b\r\nlet add a b c = a  b + c\r\n<\/pre>\n<p>However one thing we could do is implement a discriminated union, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\ntype Addition =\r\n    | Two of int * int\r\n    | Three of int * int * int\r\n\r\nlet add param =\r\n    match param with\r\n    | Two (a, b) -&gt; a + b\r\n    | Three (a, b, c) -&gt; a + b + c\r\n<\/pre>\n<p>Now to call the function using this code we&#8217;d write<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet a = add &lt;| Three(1, 2, 3)\r\n\r\n\/\/ or\r\n\r\nlet a = add (Three(1, 2, 3))\r\n<\/pre>\n<p>in C# we would use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar a = Dsl.add(Dsl.Addition.NewThree(1, 2, 3));\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>F# doesn&#8217;t support function overloads, so you cannot write the following let add a b = a + b let add a b c = a b + c However one thing we could do is implement a discriminated union, for example type Addition = | Two of int * int | Three of int [&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-2720","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\/2720","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=2720"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2720\/revisions"}],"predecessor-version":[{"id":2722,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2720\/revisions\/2722"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}