{"id":79,"date":"2012-10-19T08:31:53","date_gmt":"2012-10-19T06:31:53","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=79"},"modified":"2012-10-19T08:33:05","modified_gmt":"2012-10-19T06:33:05","slug":"records-2","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/19\/records-2\/","title":{"rendered":"More About Records"},"content":{"rendered":"<p>Since records offer methods and properties they are often used to replace objects. This is possible if there is no need to use inheritance.<\/p>\n<p>But there is a significant difference between both. References to objects are like pointers to a piece of memory whereas records are a piece of memory.<\/p>\n<p>This makes a significant difference if you are using functions that return objects and records. Let&#8217;s have a look at one example.<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"Test Object\">program TestObject;\r\n\r\n{$APPTYPE CONSOLE}\r\n\r\n{$R *.res}\r\n\r\ntype\r\n  TMyClass = class(TObject)\r\n  strict private\r\n    FNo: Integer;\r\n  public\r\n    property No: Integer read FNo write FNo;\r\n  end;\r\n\r\n  function Clone(const AClass: TMyClass): TMyClass;\r\n  begin\r\n    Result := AClass;\r\n    Result.No := AClass.No + 1;\r\n  end;\r\n\r\nvar\r\n  pClass: TMyClass;\r\n  pClone: TMyClass;\r\nbegin\r\n  pClass := TMyClass.Create;\r\n  try\r\n    pClone := Clone(pClass);\r\n    if pClone.No = pClass.No then\r\n      Writeln('Clone hasn't cloned, it uses the same instance');\r\n  finally\r\n    pClass.Free;\r\n  end;\r\n\r\nend.<\/pre>\n<p>This is really simply. The clone function returns the reference itself. This means that it <strong>doesn&#8217;t clone<\/strong>, it uses the same instance!<\/p>\n<p>Now, we will do the same with an record.<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"Test Object\">program TestRecord;\r\n\r\n{$APPTYPE CONSOLE}\r\n\r\n{$R *.res}\r\n\r\ntype\r\n  TMyRecord = record\r\n  strict private\r\n    FNo: Integer;\r\n  public\r\n    property No: Integer read FNo write FNo;\r\n  end;\r\n\r\n  function Clone(const ARecord: TMyRecord): TMyRecord;\r\n  begin\r\n    Result := ARecord;\r\n    Result.No := ARecord.No + 1;\r\n  end;\r\n\r\nvar\r\n  pRecord: TMyRecord;\r\n  pClone: TMyRecord;\r\nbegin\r\n  pClone := Clone(pRecord);\r\n  if pClone.No &lt;&gt; pRecord.No then\r\n    Writeln('Clone has really cloned');\r\n\r\nend.<\/pre>\n<p>Now you can see the differences.<\/p>\n<ol>\n<li>There is no need to create a record, the record is simply there.<\/li>\n<li>With point 1 there is no need to destroy the record.<\/li>\n<li>A function that returns a record automatically creates a new one.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Since records offer methods and properties they are often used to replace objects. This is possible if there is no need to use inheritance. But there is a significant difference between both. References to objects are like pointers to a &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/19\/records-2\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-79","post","type-post","status-publish","format-standard","hentry","category-tipsandtricks","tag-delphi"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/79","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/comments?post=79"}],"version-history":[{"count":6,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/79\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=79"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=79"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=79"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}