When using an anonymous method as a delegate the compiler should either capture referenced external variables or disallow references to them.
Currently the compiler treats anonymous delegates as closures.
The following script reproduces this nice feature.
Smart pascal source code
{ filename: B.pas }
unit B;
interface
uses
SmartCL.System;
type
TEvent = procedure;
type
TMyClass = class
private
FEvent: TEvent;
public
constructor Create;
procedure DoIt;
property Event: TEvent read FEvent write FEvent;
end;
implementation
constructor TMyClass.Create;
begin
var
x := 'hello';
FEvent := procedure
begin
WriteLn(x); // Displays 'hello'
WriteLn(ClassName); // Displays 'TMyClass'
end;
end;
procedure TMyClass.DoIt;
begin
if (Assigned(FEvent)) then
FEvent;
end;
end.
{ filename: uMain.pas }
var MyClass: TMyClass;
MyClass := TMyClass.Create;
MyClass.DoIt; // displays 'hello' 'TMyClass'