Skip to content

Instantly share code, notes, and snippets.

@Akira13641
Created July 10, 2019 01:42
Show Gist options
  • Save Akira13641/2ec32459e904932ea4299addfd42e51b to your computer and use it in GitHub Desktop.
Save Akira13641/2ec32459e904932ea4299addfd42e51b to your computer and use it in GitHub Desktop.
Delphi / FPC parameter passing comparison
program ExampleDelphi;
{$VARPROPSETTER ON}
type
TRecord = record
private
FBool: Boolean;
function GetBoolOut(out Index: NativeUInt): Boolean;
function GetBoolVar(var Index: NativeUInt): Boolean;
procedure SetBoolOut(out Index: NativeUInt; out B: Boolean);
procedure SetBoolVar(var Index: NativeUInt; var B: Boolean);
function GetBoolConstRef(const [ref] Index: NativeUInt): Boolean;
procedure SetBoolConstref(const [ref] Index: NativeUInt; const [ref] B: Boolean);
public
property OutBools[out Index: NativeUInt]: Boolean read GetBoolOut write SetBoolOut;
property VarBools[var Index: NativeUInt]: Boolean read GetBoolVar write SetBoolVar;
property ConstrefBools[const [ref] Index: NativeUInt]: Boolean read GetBoolConstRef write SetBoolConstref;
end;
function TRecord.GetBoolOut(out Index: NativeUInt): Boolean;
begin
Result := Index > 5000;
end;
function TRecord.GetBoolVar(var Index: NativeUInt): Boolean;
begin
Result := Index > 5000;
end;
procedure TRecord.SetBoolOut(out Index: NativeUInt; out B: Boolean);
begin
FBool := B;
B := Index > 5000;
end;
procedure TRecord.SetBoolVar(var Index: NativeUInt; var B: Boolean);
begin
FBool := B;
B := Index > 5000;
end;
function TRecord.GetBoolConstRef(const [ref] Index: NativeUInt): Boolean;
begin
Result := Index > 5000;
end;
procedure TRecord.SetBoolConstref(const [ref] Index: NativeUInt; const [ref] B: Boolean);
begin
FBool := B and (Index > 5000);
end;
var
R: TRecord;
I: NativeUInt = 5000;
begin
// E2033 Types of actual and formal var parameters must be identical
WriteLn(R.OutBools[5001]);
// E2033 Types of actual and formal var parameters must be identical
R.OutBools[5001] := False;
// E2033 Types of actual and formal var parameters must be identical
WriteLn(R.VarBools[5001]);
// E2033 Types of actual and formal var parameters must be identical
R.VarBools[5001] := False;
// Works fine
WriteLn(R.ConstrefBools[5001]);
// Works fine
R.ConstrefBools[5001] := False;
// Works fine
WriteLn(R.OutBools[I]);
// E2036 Variable required
R.OutBools[I] := True;
// Works fine
WriteLn(R.VarBools[I]);
// E2036 Variable required
R.VarBools[I] := False;
// Works fine
WriteLn(R.ConstrefBools[I]);
// Works fine
R.ConstrefBools[I] := True;
end.
program ExampleFPC;
{$mode Delphi}
{$VARPROPSETTER ON}
type
TRecord = record
private
FBool: Boolean;
function GetBoolOut(out Index: NativeUInt): Boolean;
function GetBoolVar(var Index: NativeUInt): Boolean;
procedure SetBoolOut(out Index: NativeUInt; out B: Boolean);
procedure SetBoolVar(var Index: NativeUInt; var B: Boolean);
function GetBoolConstRef(constref Index: NativeUInt): Boolean;
procedure SetBoolConstref(constref Index: NativeUInt; constref B: Boolean);
public
property OutBools[out Index: NativeUInt]: Boolean read GetBoolOut write SetBoolOut;
property VarBools[var Index: NativeUInt]: Boolean read GetBoolVar write SetBoolVar;
property ConstrefBools[constref Index: NativeUInt]: Boolean read GetBoolConstRef write SetBoolConstref;
end;
function TRecord.GetBoolOut(out Index: NativeUInt): Boolean;
begin
Result := Index > 5000;
end;
function TRecord.GetBoolVar(var Index: NativeUInt): Boolean;
begin
Result := Index > 5000;
end;
procedure TRecord.SetBoolOut(out Index: NativeUInt; out B: Boolean);
begin
FBool := B;
B := Index > 5000;
end;
procedure TRecord.SetBoolVar(var Index: NativeUInt; var B: Boolean);
begin
FBool := B;
B := Index > 5000;
end;
function TRecord.GetBoolConstRef(constref Index: NativeUInt): Boolean;
begin
Result := Index > 5000;
end;
procedure TRecord.SetBoolConstref(constref Index: NativeUInt; constref B: Boolean);
begin
FBool := B and (Index > 5000);
end;
var
R: TRecord;
I: NativeUInt = 5000;
begin
// Error: Variable identifier expected
WriteLn(R.OutBools[5001]);
// Error: Variable identifier expected
R.OutBools[5001] := False;
// Error: Variable identifier expected
WriteLn(R.VarBools[5001]);
// Error: Variable identifier expected
R.VarBools[5001] := False;
// Works fine
WriteLn(R.ConstrefBools[5001]);
// Works fine
R.ConstrefBools[5001] := False;
// Works fine
WriteLn(R.OutBools[I]);
// Error: Variable identifier expected
R.OutBools[I] := True;
// Works fine
WriteLn(R.VarBools[I]);
// Error: Variable identifier expected
R.VarBools[I] := False;
// Works fine
WriteLn(R.ConstrefBools[I]);
// Works fine
R.ConstrefBools[I] := True;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment