I ran a test in Rio and it works quite well:
type
TTestObj = class
public
constructor Create;
destructor Destroy; override;
end;
constructor TTestObj.Create;
begin
inherited Create;
Writeln('TTestObj.Create');
end;
destructor TTestObj.Destroy;
begin
Writeln('TTestObj.Destroy');
inherited;
end;
procedure Test;
begin
Writeln('Test started');
begin var obj := Shared.Make(TTestObj.Create)();
Writeln('obj = ', obj.ClassName);
Writeln('End of nested scope');
end;
Writeln('Test completed');
end;
Output:
Test started
TTestObj.Create
obj = TTestObj
End of nested scope
Test completed
TTestObj.Destroy
Inline var object is not destroyed at the end of scope, but at the end of the method. Still, that's something I more or less expected. Other than that, the code works fine.
For the record, this can also be achieved with the "traditional" syntax. Delphi will kindly keep the interface alive until the end of the method:
procedure Test;
var
obj: TTestObj;
begin
Writeln('Test started');
obj := Shared.Make(TTestObj.Create)();
Writeln('obj = ', obj.ClassName);
Writeln('Test completed');
end;
And there's also a bit slower but shorter variation to create a shared object:
Shared<TTestObj>.Make()
Just saying 😉