Smart pascal source code
type
TBase = class
Field: Integer;
procedure MyProc;
function Virt: Integer; virtual;
end;
type
TChild = class(TBase)
function Virt: Integer; override;
property Prop: Integer read Field;
end;
procedure TBase.MyProc;
begin
if Assigned(Self) then
begin
WriteLn(Self.ClassName);
WriteLn(Self.Virt);
end
else
WriteLn('nil');
end;
function TBase.Virt: Integer;
begin
Result := Self.Field;
end;
function TChild.Virt: Integer;
begin
Result := Self.Prop * 100;
end;
var o: TBase;
o.MyProc;
try
o.Virt;
except
on E: Exception do
WriteLn(E.Message);
end;
o := TBase.Create;
o.Field := 1;
o.MyProc;
o := TChild.Create;
o.Field := 2;
o.MyProc;
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
nil
Object not instantiated [line: 34, column: 6]
TBase
1
TChild
200
-----------------------------
{<<<<<<<<< THE END >>>>>>>>>}
function ClassName$1(Self$1) {
return "Helper1."+TObject.ClassName($Check(Self$1,""));
}
var TBase = {
$ClassName:"TBase",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
$.Field = 0;
}
,MyProc:function(Self) {
if (Self) {
WriteLn(ClassName$1($ToClassType(Self)));
WriteLn(TBase.Virt$($Check(Self,"")));
} else {
WriteLn("nil");
}
}
,Virt:function(Self) {
return $Check(Self,"").Field;
}
,Destroy:TObject.Destroy
,Virt$:function($){return $.ClassType.Virt($)}
};
var TChild = {
$ClassName:"TChild",$Parent:TBase
,$Init:function ($) {
TBase.$Init($);
}
,Virt:function(Self) {
return $Check(Self,"").Field*100;
}
,Destroy:TObject.Destroy
,Virt$:function($){return $.ClassType.Virt($)}
};
var o = null;
/* <<< main JS >>> */
TBase.MyProc(o);
try {
TBase.Virt$($Check(o,""));
} catch ($e) {
var E = $W($e);
WriteLn($Check(E,"").FMessage) }
o = TObject.Create($New(TBase));
$Check(o,"").Field = 1;
TBase.MyProc(o);
o = TObject.Create($New(TChild));
$Check(o,"").Field = 2;
TBase.MyProc(o);