Forum > Free Pascal
Feature announcement: Function References and Anonymous Functions
Thaddy:
Nice toy! Well done you all.
Here's an old Barry Kelly smartpointer example for Delphi that now compiles in FPC:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$ifdef windows}{$apptype console}{$endif}{$mode delphi}{$modeswitch functionreferences}{$modeswitch anonymousfunctions}{$warn 5036 off}uses SysUtils; type Tproc = reference to procedure; TLifetimeWatcher = class(TInterfacedObject) private FWhenDone: TProc; public constructor Create(const AWhenDone: TProc); destructor Destroy; override; end; { TLifetimeWatcher } constructor TLifetimeWatcher.Create(const AWhenDone: TProc);begin FWhenDone := AWhenDone;end; destructor TLifetimeWatcher.Destroy;begin if Assigned(FWhenDone) then FWhenDone; inherited;end; type TSmartPointer<T: class> = record strict private FValue: T; FLifetime: IInterface; public constructor Create(const AValue: T); overload; class operator Implicit(const AValue: T): TSmartPointer<T>; property Value: T read FValue; end; { TSmartPointer<T> } constructor TSmartPointer<T>.Create(const AValue: T);begin FValue := AValue; FLifetime := TLifetimeWatcher.Create(procedure begin AValue.Free; end);end; class operator TSmartPointer<T>.Implicit(const AValue: T): TSmartPointer<T>;begin Result := TSmartPointer<T>.Create(AValue);end; procedure UseIt;var x: TSmartPointer<TLifetimeWatcher>;begin x := TLifetimeWatcher.Create(procedure begin Writeln('I died.'); end);end; begin try UseIt; Readln; except on E:Exception do Writeln(E.Classname, ': ', E.Message); end;end.
simone:
Thanks to the development team. Will this new feature be available in the next release version of fpc?
PascalDragon:
--- Quote from: simone on May 27, 2022, 03:05:29 pm ---Thanks to the development team. Will this new feature be available in the next release version of fpc?
--- End quote ---
It will be in the next major release which is not yet scheduled. The next release will be a minor release (namely 3.2.4).
Bi0T1N:
I'm glad to see that it has finally arrived in FPC - good work! This should allow us to use several nice Delphi libraries with FPC. 8-)
I think you can also close the issue now.
Thaddy:
--- Quote from: Bi0T1N on May 27, 2022, 09:09:59 pm ---I'm glad to see that it has finally arrived in FPC - good work! This should allow us to use several nice Delphi libraries with FPC. 8-)
I think you can also close the issue now.
--- End quote ---
Not only that: FPC can do more as per PascalDragon's introductary notes...
I have been toying with old D2009 and XE2 examples today and 90% can be made to work in minutes. Much more than I expected.
Navigation
[0] Message Index
[#] Next page
[*] Previous page