So I'm having a case of morning daftness. Why can't record constructors be parameterless?

So I'm having a case of morning daftness. Why can't record constructors be parameterless?

After all records don't support inheritance, and there is nothing preventing you from declaring a parameterless class function called Create...

Comments

  1. From the help:

    "Records are constructed automatically, using a default no-argument constructor, but classes must be explicitly constructed. Because records have a default no-argument constructor, any user-defined record constructor must have one or more parameters."

    http://docwiki.embarcadero.com/RADStudio/XE5/en/Structured_Types ]

    ReplyDelete
  2. My understanding, right or wrong, is that records already have a hidden parameterless constructor. A possible "work around" that is used in the RTL (Rtti.pas) is that you make a class function called Create:

    TMyRecord = record
    public
      class function Create:TMyRecord; static;
    end;

    ReplyDelete
  3. Kiss Nándor Well, still does not explain why you can't have a parameter-less constructor IMHO. If defined it could call the internal record initialization procedure first, then run whatever you want.

    I'm guessing it's down to some internal voodoo they did when implementing this in the compiler.

    ReplyDelete
  4. It has puzzled me as well.  I also miss using the old object type as an inheritable and extensible record type which could be aggregated. It was very handy for dealing with binary streams.

    ReplyDelete
  5. A buddy suggested this is one of those .NET limitations they decided to port to Delphi. Sounds very plausible.

    ReplyDelete
  6. Well. I think records should be just that, no methods or properties or anything. Just structures to hold and map a memory region. If you want constructors and function and properties then build a class damnit. Hmph! :-P

    In all seriousness, I don't want every damned thing to be an object - learn to use memory properly ffs - and pointers, whilst I'm moaning about the general state of programming today. Learn to use pointers properly, wonderful things pointers, none of this managed code bs :-)

    ReplyDelete
  7. Funny enough that TObject.CleanupInstance calls _FinalizeRecord to finalize the managed fields :)

    ReplyDelete
  8. Paul Foster I must admit I quite like the C++ way. Allocate on stack by default, otherwise jump through the smart_ptr hoops. No real difference between a struct and an object there.

    ReplyDelete
  9. There is very little to like about c++ for me :-P

    ReplyDelete
  10. Paul Foster Each to his own. I quite like C++ for a lot of things, after spending some time with it the last 5 years.

    ReplyDelete
  11. Asbjørn Heid you have my sympathies :-P

    I jest, I think I am just too long in the tooth to get on with c++, too long in Delphi land perhaps.

    ReplyDelete
  12. Paul Foster I had been programming almost exclusively in Pascal/Delphi for my first 10 years before I seriously attempted C++. I had much the same view as you. I found it hard to read and confusing.

    I quickly warmed up to it though, and while it has it's interesting points, and a lot of sharp knifes to drop on your feet, I do think a lot of the concepts are superior to what Delphi can offer. Stack based allocation and RAII is one of them.

    ReplyDelete
  13. Asbjørn Heid I have done some fairly major work in c++, mostly working on open source code, to be honest it could just be having to use visual studio thats tainting it for me :-)

    ReplyDelete
  14. Paul Foster I've only used vs2008 and later. I find vs2010 to be a vastly superior code editior to Delphi, though I really enjoy SyncEdit. I don't do any GUI stuff in vs tho, just a bit in Qt (using Qt Designer).

    ReplyDelete
  15. I think the original argument from CodeGear (?) was that programmers would expect the user-defined no-parameter record constructor to be automatically called by the compiler.

    I wouldn't - and I think they were wrong. Records do not have a constructor by default, there is just compiler magic to initialize some (magic types) fields.

    ReplyDelete
  16. Hallvard Vassbotn Well, that also makes sense and is an equally stupid reason. This is why we can't have nice things in Delphi :(

    ReplyDelete

Post a Comment