tfuncref2.pp 664 B

12345678910111213141516171819202122232425262728293031323334
  1. { %OPT=-gh }
  2. { function reference with compatible signatures can be assigned to each other }
  3. program tfuncref2;
  4. {$mode objfpc}{$H+}
  5. {$modeswitch functionreferences}
  6. type
  7. TFunc1 = reference to function(aArg: LongInt): String;
  8. TFunc2 = reference to function(aArg: LongInt): String;
  9. TTest = class(TInterfacedObject, TFunc1)
  10. function Invoke(aArg: LongInt): String;
  11. end;
  12. function TTest.Invoke(aArg: LongInt): String;
  13. begin
  14. Str(aArg, Result);
  15. end;
  16. var
  17. f1: TFunc1;
  18. f2: TFunc2;
  19. begin
  20. {$if declared(HaltOnNotReleased)}
  21. HaltOnNotReleased:=True;
  22. {$endif}
  23. f1 := TTest.Create;
  24. f2 := f1;
  25. f1 := Nil;
  26. if f2(42) <> '42' then
  27. Halt(1);
  28. end.