{"id":1196,"date":"2014-01-29T14:38:15","date_gmt":"2014-01-29T14:38:15","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1196"},"modified":"2014-01-29T14:38:15","modified_gmt":"2014-01-29T14:38:15","slug":"properties-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/properties-in-f\/","title":{"rendered":"Properties in F#"},"content":{"rendered":"<p>We&#8217;re talking class properties&#8230;<\/p>\n<p><strong>Automatic Properties<\/strong><\/p>\n<p>Let&#8217;s start with the one I seem to have most trouble remembering, especially as it&#8217;s a different syntax to the other properties. We&#8217;re talking <strong>automatic properties<\/strong>.<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\ntype Rectangle()= \r\n   member val length = 0 with get, set\r\n   member val width = 0 with get, set\r\n<\/pre>\n<p>The key differences to the other property syntax, i.e. properties with backing fields (for example) are, the addition of the <em>val<\/em> keyword, the lack of a self-identifier, the initialization expression, i.e. the <em>length<strong> = 0<\/strong><\/em> and the <em>get, set<\/em>, i.e. no <em>and<\/em>.<\/p>\n<p>So the above creates a type Rectangle and two auto properties, length and width. Both are initialized to 0. <\/p>\n<p><strong>Non-automatic properties<\/strong><\/p>\n<p>Let&#8217;s start with the syntax for a property with both a getter and setter. Let&#8217;s start with the version of the above automatic property example but with backing fields<\/p>\n<p><em>Note: the use of <strong>this<\/strong> and <strong>value<\/strong> as the identifier and parameter respectively is just to use C# style naming and obviously can be changed to suit<\/em><\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\ntype Rectangle() =\r\n   let mutable l = 0\r\n   let mutable w = 0\r\n   member this.length \r\n      with get() = l\r\n      and set value = l &lt;- value\r\n   member this.width\r\n      with get() = w\r\n      and set value = w &lt;- value\r\n<\/pre>\n<p>The alternative syntax for the above is as follows<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\ntype Rectangle() =\r\n   let mutable l = 0\r\n   let mutable w = 0\r\n   member this.length = l\r\n   member this.length with set value = l &lt;- value\r\n   member this.width = w\r\n   member this.width with set value = w &lt;- value\r\n<\/pre>\n<p><strong>Using properties<\/strong><\/p>\n<p>And just for completeness, let&#8217;s look at how we use properties on a class. As in C# we can simply use the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet r = new Rectangle()\r\nr.length &lt;- 100\r\n<\/pre>\n<p>or using the F# equivalent of the C# initializer<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet r = new Rectangle(length = 100)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;re talking class properties&#8230; Automatic Properties Let&#8217;s start with the one I seem to have most trouble remembering, especially as it&#8217;s a different syntax to the other properties. We&#8217;re talking automatic properties. type Rectangle()= member val length = 0 with get, set member val width = 0 with get, set The key differences to the [&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-1196","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\/1196","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=1196"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1196\/revisions"}],"predecessor-version":[{"id":1199,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1196\/revisions\/1199"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}