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...
After all records don't support inheritance, and there is nothing preventing you from declaring a parameterless class function called Create...
I'd like to know that too.
ReplyDeleteFrom the help:
ReplyDelete"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 ]
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:
ReplyDeleteTMyRecord = record
public
class function Create:TMyRecord; static;
end;
Found this:
ReplyDeletehttp://sergworks.wordpress.com/2012/03/13/record-constructors-in-delphi/
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.
ReplyDeleteI'm guessing it's down to some internal voodoo they did when implementing this in the compiler.
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.
ReplyDeleteA buddy suggested this is one of those .NET limitations they decided to port to Delphi. Sounds very plausible.
ReplyDeleteWell. 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
ReplyDeleteIn 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 :-)
Funny enough that TObject.CleanupInstance calls _FinalizeRecord to finalize the managed fields :)
ReplyDeletePaul 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.
ReplyDeleteThere is very little to like about c++ for me :-P
ReplyDeletePaul Foster Each to his own. I quite like C++ for a lot of things, after spending some time with it the last 5 years.
ReplyDeleteAsbjørn Heid you have my sympathies :-P
ReplyDeleteI jest, I think I am just too long in the tooth to get on with c++, too long in Delphi land perhaps.
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.
ReplyDeleteI 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.
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 :-)
ReplyDeletePaul 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).
ReplyDeleteI 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.
ReplyDeleteI 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.
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