[fpc-pascal] Primitive Record Wrappers
Michalis Kamburelis
michalis.kambi at gmail.com
Sat Feb 20 06:24:18 CET 2016
2016-02-18 15:43 GMT+01:00 Mazola Winstrol <mazofeifer at gmail.com>:
> Em 18/02/2016 10:12, "Marco van de Voort" <marcov at stack.nl> escreveu:
>>
>> In our previous episode, Mazola Winstrol said:
>> > end;
>> >
>> >
>> > Suppose that this class represent data of the Person table in a sql
>> > database. Suppose that there is records where the column "BirthDate" is
>> > null. How do i represent a null "BirthDate" in my object?
>>
>> Keep using variants instead of typed data.
>>
>
> I think this is not a good alternative. As the variant are untyped, should
> the consumers of the person class assume that BirthDate are filled as a
> formated date string '01/01/1980' or as a native tdatetime?
>
> I think it would be better using record wrappers to primitive types. See
> http://blogs.embarcadero.com/abauer/2008/09/18/38869.
>
> What do you think about that?
Personally, I like the Nullable<T> idea from C#, which is basically a
generic record adding a "HasValue: boolean" to "Value: T". The
type-safety is nice, and it's sometimes very useful (when you cannot
"cheat" and say that "empty string" or "zero value" always means
"uninitialized"). Looking at your link
http://blogs.embarcadero.com/abauer/2008/09/18/38869 , this is exactly
trying to implement Nullable<T> in Object Pascal.
The major problem there is that it's difficult to force it to be
always initialized with zeroes. Currently, a non-global variable of
unmanaged type can always be filled with memory garbage, as explained
in other thread. The trick in
http://blogs.embarcadero.com/abauer/2008/09/18/38869 to use interfaces
feels ugly, it's an over-use of the "interface" specifics in Object
Pascal.
I remember a thread on fpc-devel "Pascal Smart Pointers Idea + ARC
implementation" where the original author (Maciej Izak) was already
working to overcome this:
https://www.mail-archive.com/fpc-devel@lists.freepascal.org/msg33172.html
. His reasons was similar (force the contents to be initialized with
zero) and his solution felt cleaner -- introduce "class operator
Initialize" where you can initialize your record.
Any chance that this would be added/merged to FPC one day?:)
Regards,
Michalis
More information about the fpc-pascal
mailing list