> On Jun 23, 2018, at 3:09 PM, leledumbo via fpc-pascal 
> <fpc-pascal@lists.freepascal.org> wrote:
> 
> Those are two different things actually: Declaration time initialization for
> variables vs default record field values. The former doesn't cause confusion
> because the value and the variable are in the same place, but the latter
> does, as Sven said: "On the other hand to know whether the fields are
> initialized or not you'd need to navigate to the type which means leaving
> your current zone of development.”

Why would you be confused if a record set a default state? If you don’t know 
the fields in your record you’re in trouble anyways because you don’t know the 
state of your program.

var
  r: TVec2;
begin
  // the record is either A) garbage or B) a default value.
  // if you can’t remember the default values you set then
  // by all means do r := TVec2.Zero every time. Have fun
  // making class procedure Zero; methods for all your custom methods also

or just force default init with a keyword;

var
  r: TVec2; default;
begin
  // now I know r = {0,0} because I set those values


….

type
        TVec2 = record
                x: integer = 0;
                y: integer = 0;
        end;

> 
> Now suppose you have that feature a record type T in unit A, then a code
> somewhere that uses unit A has a variable V of type T declared. How could
> you be sure what value that V holds? At best you have to initialize it right
> there anyway, making the default value useless. Certainly don't imagine
> you're the writer of unit A who knows that T has some default values. Other
> languages POV are mostly simply ease of typing, they don't think the
> consequences this far.

Because Pascal doesn’t init by default you would be effectively overriding that 
behavior by adding default fields. In worst case scenario you’re still in the 
same place as before, i.e. you don’t know what the state is of your data. The 
programmer needs to know what they’re doing and what the contents of their data 
is regardless. Pascal's default behavior is “do noting” and default fields 
would just override that.

Swift FORCES the init of structures because they follow your general line of 
thinking which is “programers don’t know what the contents of their data are, 
so the language needs to guarantee a default state for them”. I hate that about 
Swift because it hold your hands at all times and adds enormous amounts of 
friction into the process. No fun at all.

Now in 3.1.1 we can use init operators to guarantee records are in a default 
state upon use. This is great but they add extra boiler plate code in the 
implementation section.

type
        TVec2 = record
                x, y: integer;
                class operator Initialize(var rec: TVec2);
        end;


class operator TVec2.Initialize(var rec: TVec2);
begin
        rec.x := 0;
        rec.y := 0;
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