{"id":1180,"date":"2014-01-25T14:48:06","date_gmt":"2014-01-25T14:48:06","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1180"},"modified":"2014-01-25T14:50:39","modified_gmt":"2014-01-25T14:50:39","slug":"records-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/records-in-f\/","title":{"rendered":"Records in F#"},"content":{"rendered":"<p><em>A far more comprehensive and frankly much better post on the subject of records can be found <a href=\"http:\/\/fsharpforfunandprofit.com\/posts\/records\/\" title=\"Records\" target=\"_blank\">here<\/a>.<\/p>\n<p>However this post is an attempt to distil things down to some of the basics.<\/em><\/p>\n<p><strong>Declaring a record type<\/strong><\/p>\n<p>Records can be thought of as a cross between a tuple with named fields and a specialised class. Basically the syntax is more in keeping with a class type but the specialisations are that, all properties are automatically exposed, you cannot define constructors as per classes and they have <em>structural equality semantics<\/em> as opposed to a classes <em>reference equality semantics<\/em>.<\/p>\n<p>Let&#8217;s declare a record type<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\ntype Person = { Name : string; Age : int }\r\n<\/pre>\n<p>Now to use this we simple write the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet p = { Name = &quot;Bob&quot;; Age = 43 }\r\n<\/pre>\n<p>So as you see we do not need to actually use the type name <em>Person<\/em> in the declaration (hence it appears similar to a tuple but with named properties and ofcourse using {&#8230;} instead of (&#8230;)). <\/p>\n<p>What if we declared the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\ntype Person = { Name : string; Age : int }\r\ntype Employee = { Name : string; Age : int }\r\n\r\nlet p = { Name = &quot;Bob&quot;; Age = 43 }\r\n<\/pre>\n<p>What type is p ?<\/p>\n<p>The value p will actually be assigned the type Employee as this was the last record declared. So we need a way to explicitly declare the type we want p to be (in the example above). This is done in the following way<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet p = { Person.Name = &quot;Bob&quot;; Age = 43 }\r\n<\/pre>\n<p>Now p is of type Person.<\/p>\n<p>You will need to assign each of the named properties when creating a value of a record.<\/p>\n<p>To access the properties, we simply use dot notation and the property name, thus<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet p = { Person.Name = &quot;Bob&quot;; Age = 43 }\r\n\r\nlet n = p.Name\r\n<\/pre>\n<p><strong>Deconstructing a record<\/strong><\/p>\n<p>We can also <em>deconstruct<\/em> a record to it&#8217;s constituent value as follows<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet p = { Person.Name = &quot;Bob&quot;; Age = 43 }\r\n\r\nlet { Name = n; Age = a } = p\r\n<\/pre>\n<p>The above <em>let<\/em> looks slightly odd in that we appear to be assigning the value n to Name whereas it&#8217;s actually the other way around in essence.<\/p>\n<p>Again, if you have more than one type which looks the same (as Person and Employee) you can again prefix the deconstruction to ensure the type is matched for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet { Person.Name = n; Age = a } = p\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A far more comprehensive and frankly much better post on the subject of records can be found here. However this post is an attempt to distil things down to some of the basics. Declaring a record type Records can be thought of as a cross between a tuple with named fields and a specialised class. [&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-1180","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\/1180","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=1180"}],"version-history":[{"count":10,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1180\/revisions"}],"predecessor-version":[{"id":1191,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1180\/revisions\/1191"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}