Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:00:58+00:00 2026-05-27T12:00:58+00:00

After asking this question about interface fields in records I assumed that the following

  • 0

After asking this question about interface fields in records I assumed that the following would work (notice the assertion):

type
  TRec <T> = record
    Intf : IInterface;
  end;

  TTestClass = class
  public
    function ReturnRec : TRec <Integer>;
  end;

  // Implementation 
  function TTestClass.ReturnRec : TRec <Integer>;
  begin
    Assert (Result.Intf = nil);    // Interface field in record should be initialized!
    Result.Intf := TInterfacedObject.Create;
  end;

I tested this with the following code:

  for I := 1 to 1000 do
    Rec := Test.ReturnRec;

and the assertion fails!

Where’s my mistake here? What assumption is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:00:59+00:00Added an answer on May 27, 2026 at 12:00 pm

    Function

    function ReturnRec: TRec<Integer>;
    

    Is semantically equal to procedure

    procedure ReturnRec(var Result: TRec<Integer>);
    

    [I’m pretty sure that somebody from Embarcadero, probably Barry Kelly or Alan Bauer stated this somewhere but I can’t find the reference at the moment.]

    In the second case, the compiler assumes that the record will be initialized (if necessary) before it is passed to the ReturnRec and doesn’t create any initialization code for rec inside ReturnRec. I’m assuming that the same code path inside compiler is taken for the first example and that’s why the Result is not initialized.

    Anyway, the solution is simple:

    function TTestClass.ReturnRec : TRec <Integer>;
    begin
      Result.Intf := TInterfacedObject.Create;
    end;
    

    Just assume that compiler knows what it’s doing and assign the interface and everything will work just fine.

    EDIT

    The problem you have occurs from the ‘for’ loop. Your code

    for I := 1 to 1000 do
      Rec := Test.ReturnRec;
    

    is compiled into something like this:

    var
      result: TRec<Integer>;
    
    Initialize(result);
    for I := 1 to 1000 do begin
      Test.ReturnRec(result);
      rec := result;
    end;
    

    That is why you are reusing same record all over and that is why Result.Intf is uninitialized only the first time.

    EDIT2

    You can trick the compiler by moving t.ReturnRec call out from the loop into a separate method.

    procedure GetRec(t: TTest; var rec: TRec);
    begin
      rec := t.ReturnRec;
    end;
    
    for i := 1 to 1000 do
      GetRec(t, rec);
    

    Now the hidden result variable lives in the GetRec procedure and is initialized every time GetRec is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please note that this is asking a question about constructors, not about classes which
I understand that this question is similar to others asking about Java Properties, and
Hello yeah I'm asking this question a second time, sorry about that but I
After asking this question and apparently stumping people, how's about this for a thought--
After asking this question about authorization, I've added a new custom attribute in an
After asking this question I was advised not to check in binaries into subversion.
After asking this question ( XAMPP Mercurial installation on Windows Apache --> HgWebDir.cgi Script
After asking a question about sending 304 Not Modified for images stored in the
This might seem like a silly question, but after asking some questions on stackoverflow
After asking so many question about android socket programming and getting valuable answers from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.