Skip to content

Nested Function Capture doesn't work in Generic Function

Summary

When trying to capture a nested function of a generic function, FPC returns an error that the symbol can not be captured

System Information

FPC Trunk, Windows 10

Example Project

program Project1;

{$mode objfpc}{$H+}
{$ModeSwitch nestedprocvars}
{$ModeSwitch functionreferences}

type
  TIntFunction = reference to function: Integer;

// Works
function FourtyTwo(const AParam: Integer): TIntFunction;
function Helper: Integer;
begin
  Result := 42;
end;
begin
  Result := @Helper
end;

// Error
generic function GenericFourtyTwo<T>: TIntFunction;
function Helper: Integer;
begin
  Result := 42;
end;
begin
  Result := @Helper
end;

begin
end.

Project1.pas

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information