[fpc-pascal] with in classes/records

Sven Barth pascaldragon at googlemail.com
Sun Sep 9 21:18:44 CEST 2018


Am 09.09.2018 um 16:16 schrieb Michael Van Canneyt:
>
>
> On Sun, 9 Sep 2018, Ryan Joseph wrote:
>
>> It seems syntacticly possible that default properties could be 
>> recursive by having a default property reference a record/class with 
>> another default property. Should that be allowed?
>
> I don't think so. Let's start with 1 level.
> For default arrays it's also only one level deep.
Ehm... news flash:

=== code begin ===

{$mode objfpc}

type
   TTest1 = class
   private
     fField: LongInt;
     function GetStuff(Index: Integer): Integer;
   public
     property Stuff[Index: Integer]: Integer read GetStuff; default;
   end;

   TTest2 = class
   private
     function GetTest(Index: Integer): TTest1;
   public
     property Test[Index: Integer]: TTest1 read GetTest; default;
   end;

{ TTest2 }

function TTest2.GetTest(Index: Integer): TTest1;
begin
   Result := TTest1.Create;
   Result.fField := Index;
end;

{ TTest1 }

function TTest1.GetStuff(Index: Integer): Integer;
begin
   Result := Index * fField;
end;

var
   Test: TTest2;
begin
   Test := TTest2.Create;
   Writeln(Test[21][2]);
end.

=== code end ===

Compiles and prints

=== output begin ===

42

=== output end ===

Though one can argue that default array properties are used explicitely 
using the "[…]"-syntax while a non-array default property is 
automatically triggered with ".".

Regards,
Sven



More information about the fpc-pascal mailing list