Re: [fpc-pascal] does Advanced Record constructor automatically zero all the memory space of that record?

2013-10-04 Thread Sven Barth

On 04.10.2013 14:48, Dennis Poon wrote:



 You can use either FillChar as you did or use Self :=
Default(TTest); (default is a compiler intrinsics that returns a 0
value of the type you passed in, e.g. Nil for classes, '' for strings,
0 for ordinals and for records all fields are set to 0)

How can I make it so that I only do the Self := Default() once in the
parent class constructor and don't have to repeat it in all its
descendant class constructor.


Why would you want to do that? Classes are already zero initialized when 
the constructor is entered.
Also using Self := Default(...) inside a class would not do what you 
expect it to do. Self is a class reference and thus Default(...) of 
the same type would return Nil and thus set your Self for the 
current method call to Nil (so you could no longer access the class 
instance at all).



I noticed in your example, you have to specify the class name TTest but
that implies in every descendant class,  I have to replace it with the
Descendant class name.

Can I do
   self := Default(ClassType) ?
hoping it will return the descendant's class type?


Default expects a type identifier and nothing else. I currently don't 
know whether Delphi supports more. But in any way it wouldn't change 
much as Default(TObject) = Default(TSomeClass) = 
Default(TSomeChildClass) = Nil.


Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] does Advanced Record constructor automatically zero all the memory space of that record?

2013-10-04 Thread Dennis Poon

Thanks a lot for clarification.

Dennis

Sven Barth wrote:

On 04.10.2013 14:48, Dennis Poon wrote:



You can use either FillChar as you did or use Self :=
Default(TTest); (default is a compiler intrinsics that returns a 0
value of the type you passed in, e.g. Nil for classes, '' for strings,
0 for ordinals and for records all fields are set to 0)

How can I make it so that I only do the Self := Default() once in the
parent class constructor and don't have to repeat it in all its
descendant class constructor.


Why would you want to do that? Classes are already zero initialized 
when the constructor is entered.
Also using Self := Default(...) inside a class would not do what you 
expect it to do. Self is a class reference and thus Default(...) 
of the same type would return Nil and thus set your Self for the 
current method call to Nil (so you could no longer access the class 
instance at all).



I noticed in your example, you have to specify the class name TTest but
that implies in every descendant class,  I have to replace it with the
Descendant class name.

Can I do
   self := Default(ClassType) ?
hoping it will return the descendant's class type?


Default expects a type identifier and nothing else. I currently don't 
know whether Delphi supports more. But in any way it wouldn't change 
much as Default(TObject) = Default(TSomeClass) = 
Default(TSomeChildClass) = Nil.


Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


-
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2013.0.3408 / Virus Database: 3222/6723 - Release Date: 10/04/13


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] does Advanced Record constructor automatically zero all the memory space of that record?

2013-09-24 Thread Dmitry Boyarintsev
If in doubt, why not to use the old-school records and an
init-value procedure.

thanks,
Dmitry

On Tuesday, September 24, 2013, Dennis Poon wrote:

 **
 if not, is there a clean and easy way to initialize the entire record
 memory space to zeros e.g.

 constructor TMyRecord.Create(TheValue : Integer);
 begin
   FillChar(self, sizeof(Self), 0);
   Value := TheVal;
 end;

 Is there a Self variable for advanced record methods/constructors ?

 By the way, is there an ultimate inherited constructor Create for all
 advanced record types?

 I cannot find any info by googling, so had to ask for your help here.

 Thanks in advanced.


 Dennis

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] does Advanced Record constructor automatically zero all the memory space of that record?

2013-09-24 Thread Sven Barth
does Advanced Record constructor automatically zero all the memory 
space of that record?

No, it does not. Neither in Delphi nor in FPC.

Am 24.09.2013 12:36, schrieb Dennis Poon:
if not, is there a clean and easy way to initialize the entire record 
memory space to zeros e.g.


constructor TMyRecord.Create(TheValue : Integer);
begin
  FillChar(self, sizeof(Self), 0);
  Value := TheVal;
end;
You can use either FillChar as you did or use Self := Default(TTest); 
(default is a compiler intrinsics that returns a 0 value of the type you 
passed in, e.g. Nil for classes, '' for strings, 0 for ordinals and for 
records all fields are set to 0)

Is there a Self variable for advanced record methods/constructors ?

Yes.
By the way, is there an ultimate inherited constructor Create for 
all advanced record types?

No.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] does Advanced Record constructor automatically zero all the memory space of that record?

2013-09-24 Thread Dennis Poon



Sven Barth wrote:
does Advanced Record constructor automatically zero all the memory 
space of that record?

No, it does not. Neither in Delphi nor in FPC.

Am 24.09.2013 12:36, schrieb Dennis Poon:
if not, is there a clean and easy way to initialize the entire record 
memory space to zeros e.g.


constructor TMyRecord.Create(TheValue : Integer);
begin
  FillChar(self, sizeof(Self), 0);
  Value := TheVal;
end;
You can use either FillChar as you did or use Self := 
Default(TTest); (default is a compiler intrinsics that returns a 0 
value of the type you passed in, e.g. Nil for classes, '' for strings, 
0 for ordinals and for records all fields are set to 0)

Is there a Self variable for advanced record methods/constructors ?

Yes.
By the way, is there an ultimate inherited constructor Create for 
all advanced record types?

No.

Regards,
Sven



Thanks for your help.

Are the above info. available anywhere in FPC's web site that I 
overlooked?  If not, maybe it should be added to its wiki.


Dennis
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] does Advanced Record constructor automatically zero all the memory space of that record?

2013-09-24 Thread Sven Barth

Am 24.09.2013 16:40, schrieb Dennis Poon:



Sven Barth wrote:
does Advanced Record constructor automatically zero all the memory 
space of that record?

No, it does not. Neither in Delphi nor in FPC.

Am 24.09.2013 12:36, schrieb Dennis Poon:
if not, is there a clean and easy way to initialize the entire 
record memory space to zeros e.g.


constructor TMyRecord.Create(TheValue : Integer);
begin
  FillChar(self, sizeof(Self), 0);
  Value := TheVal;
end;
You can use either FillChar as you did or use Self := 
Default(TTest); (default is a compiler intrinsics that returns a 0 
value of the type you passed in, e.g. Nil for classes, '' for 
strings, 0 for ordinals and for records all fields are set to 0)

Is there a Self variable for advanced record methods/constructors ?

Yes.
By the way, is there an ultimate inherited constructor Create for 
all advanced record types?

No.

Regards,
Sven



Thanks for your help.

Are the above info. available anywhere in FPC's web site that I 
overlooked?  If not, maybe it should be added to its wiki.
Both points should be added to the documentation, but since they both 
are implemented only in trunk this is not the case yet.


Default() is also mentioned here: 
http://wiki.lazarus.freepascal.org/FPC_New_Features_Trunk#New_compiler_intrinsic_Default


Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal