It’s a real pain setting default values for records in Pascal at compile time. 
Every other language seems to get this right but it’s a constant issue in 
Pascal. The new "class operator Initialize” feature goes a long way in helping 
this but it’s still a little annoying that the function is down in the 
implementation and taking up space plus requires scrolling etc… Static class 
methods help also but again the function is detached from the record and needs 
to be called manually.

Why can’t we just do something simple like this? A simple meta programming hack 
to dump the “default” section at the start of the scope the variable is 
declared in so we don’t have to do this ourselves every time. It probably 
breaks down for init’ing records but for compiler types it seems pretty simple.

type
        TMyRecord = record
                public
                        i: integer;
                        s: string;
                default
                        i := 1;
                        s := 'foo';
        end;

var
        myRec: TMyRecord; default; // if no default section is specified then 
use FillChar(this, sizeof(this), 0);
begin
        // the compiler copy/pastes the variables "default" section at start of 
the scope
        myRec.i := 1;
        myRec.s := 'foo';
end;



Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to