Smart pascal source code
type
TClass1 = class
Field: String;
procedure Anything;
property Prop: String read Field write Field;
end;
procedure TClass1.Anything;
begin
WriteLn('here');
end;
type
TClass2 = class
fClass1Item: TClass1;
function NeedClass1: TClass1;
property Class1Item: TClass1 read fClass1Item;
end;
function TClass2.NeedClass1: TClass1;
begin
if not Assigned(fClass1Item) then
begin
fClass1Item := TClass1.Create;
WriteLn('Class1 needed');
end;
Result := fClass1Item;
end;
var class2Item: TClass2;
class2Item := TClass2.Create;
class2Item.NeedClass1.Anything;
class2Item.Class1Item.Anything;
class2Item.Class1Item.Field := 'hello';
WriteLn(class2Item.Class1Item.Field);
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
Class1 needed
here
here
hello
-----------------------------
{<<<<<<<<< THE END >>>>>>>>>}
var TClass2 = {
$ClassName:"TClass2",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
$.fClass1Item = null;
}
,NeedClass1:function(Self) {
var Result = null;
if (!$Check(Self,"").fClass1Item) {
$Check(Self,"").fClass1Item = TObject.Create($New(TClass1));
WriteLn("Class1 needed");
}
Result = $Check(Self,"").fClass1Item;
return Result
}
,Destroy:TObject.Destroy
};
var TClass1 = {
$ClassName:"TClass1",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
$.Field = "";
}
,Anything:function(Self) {
WriteLn("here");
}
,Destroy:TObject.Destroy
};
var class2Item = null;
/* <<< main JS >>> */
class2Item = TObject.Create($New(TClass2));
TClass1.Anything(TClass2.NeedClass1(class2Item));
TClass1.Anything($Check(class2Item,"").fClass1Item);
$Check($Check(class2Item,"").fClass1Item,"").Field = "hello";
WriteLn($Check($Check(class2Item,"").fClass1Item,"").Field);