[fpc-pascal] how to use Default to replace this ' FillChar(aRecord, sizeof(aRecord), 0);

Lars noreply at z505.com
Mon Dec 5 10:35:49 CET 2016


Do you know about this feature:

var
  SomeRec: TSomeRecord = ();

This initializes the record without needing fillchar.
I assume your "Default()" call initializes the record initially. You can
eliminate the default() call by initializing the record as part of your
declaration.

Some criticize this as being C++ like and not pascalish. I find it to be a
contract set up at the declaration stage without needing for ugly fillchar
hacks which seem like a mess... But old pascal programmers are used to and
like fillchar.

However, my email here may have nothing to do with what you are trying to
accomplish...  As you want a some kind of generic function to fillchar any
record, I guess... My solution above, is to use () to fillchar it to 0
without requiring any call to fillchar. If you want to add values other
than zero then you can set them up inside the initialization instead of a
blank ()

An interesting trivia note: delphi 5 doesn't seem to allow initialized
record declarations in local variables inside a procedure but allows it
for globals or unit variables...

Also the fpc compiler gives a warning:
Warning: Some fields coming after "" were not initialized

When using () to initialize a record... I'm not sure if this warning is
correct, as theoretically the compiler should in fact set all record
values to zero in the case of ()

Should this warning be looked at as a possible erroneous warning?

Again, sorry if my answer to your email has nothing at all to do with what
you are trying to accomplish!



On Mon, December 5, 2016 12:23 am, Dennis wrote:
> In this old statement, I don't need to know the type of aRecord
> but if I want to use Default, I have to know the exact type of the varaible
> aRecord and pass it to Default(aRecordType) e.g. aRecord :=
> Default(aRecordType)  //where var  aRecord : aRecordType
>
>
> Is it possible to use default without knowing the variable type? (it is
> because sometimes the type get renamed)
>
> Dennis
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
>




More information about the fpc-pascal mailing list