{"id":2218,"date":"2014-07-28T21:38:26","date_gmt":"2014-07-28T21:38:26","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2218"},"modified":"2014-07-28T21:38:26","modified_gmt":"2014-07-28T21:38:26","slug":"type-extensions-in-f-2","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/type-extensions-in-f-2\/","title":{"rendered":"Type extensions in F#"},"content":{"rendered":"<p>We can extend F# types by adding the equivalent of C# extension methods or something similar to partial classes in C#.<\/p>\n<p>Let&#8217;s not waste time but instead get straight into the code<\/p>\n<p><em>The type extension syntax is taken from <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd233211.aspx\" title=\"Type Extensions (F#)\" target=\"_blank\">Type Extensions (F#)<\/a> which is well worth a read<\/em><\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\ntype typename with\r\n   member self-identifier.member-name =\r\n      body\r\n   ...\r\n   &#x5B; end ]\r\n<\/pre>\n<p>[ end ] is optional in lightweight syntax.<\/p>\n<p>Here&#8217;s an example of a type method for the System.String type, we&#8217;re going to add a simple Reverse function<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\ntype System.String with\r\n    member __.Reverse = String(__.ToCharArray() |&gt; Array.rev)\r\n<\/pre>\n<p>also we can create new functions like adding to a partial class <\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\ntype System.String with\r\n    member __.Append s = __.Substring(0) + s\r\n<\/pre>\n<p>A slightly pointless method, but at the time of writing I couldn&#8217;t think of anything more interesting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We can extend F# types by adding the equivalent of C# extension methods or something similar to partial classes in C#. Let&#8217;s not waste time but instead get straight into the code The type extension syntax is taken from Type Extensions (F#) which is well worth a read type typename with member self-identifier.member-name = body [&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-2218","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\/2218","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=2218"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2218\/revisions"}],"predecessor-version":[{"id":2224,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2218\/revisions\/2224"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}