Recent

Author Topic: Generic functions/methods in delphi mode  (Read 330 times)

XiJinping

  • New member
  • *
  • Posts: 9
Generic functions/methods in delphi mode
« on: September 08, 2019, 08:47:39 am »
Hello all.

I have a function like:

Code: Pascal  [Select]
  1.   {$ifdef fpc}
  2.   {$mode delphi}{$H+}
  3.   {$endif fpc}
  4.   ...
  5.   function SetResultValue<T: class>(Token: TOptValue<T>; Tmp: T): TFail;

but I get "parser.pas(101,28) Fatal: Syntax error, ">" expected but ":" found". I tried also without constraint "class", but then I get "parser.pas(101,47) Error: Identifier not found "T"" and "parser.pas(101,48) Error: Type identifier expected". Also I tried to move this function in a class (initially I need it there). But it leads to errors too. How can I create generic function/method in DELPHI mode?

Thaddy

  • Hero Member
  • *****
  • Posts: 8973
Re: Generic functions/methods in delphi mode
« Reply #1 on: September 08, 2019, 09:21:01 am »
Working example:
Code: Pascal  [Select]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif fpc}
  2. // the above is a bit dangerous because it won't work in Delphi, only in FPC mode Delphi.
  3. // Here's code in Delphi style syntax:
  4. uses classes;
  5. type
  6.   TFail =  type Boolean;
  7.   TOptValue<T:class> = record
  8.     value:T
  9.   end;
  10.  
  11.   function SetResultValue<T: class>(Token: TOptValue<T>; Tmp: T): TFail;
  12.   begin
  13.     result := true;
  14.     if Token.Value = Tmp then Result := false;
  15.   end;
  16.  
  17. begin
  18. end.

Note this code needs FPC 3.2.0 to work and unlike maybe suggested by the mode it DOES NOT WORK in Delphi!!! Delphi doesn't support it.
See: https://wiki.freepascal.org/FPC_New_Features_3.2#Support_for_generic_routines
« Last Edit: September 08, 2019, 09:31:28 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

XiJinping

  • New member
  • *
  • Posts: 9
Re: Generic functions/methods in delphi mode
« Reply #2 on: September 08, 2019, 09:32:57 am »
Oh, I got it, I am on FPC 3.0.4. Thank you!

ASerge

  • Hero Member
  • *****
  • Posts: 1409
Re: Generic functions/methods in delphi mode
« Reply #3 on: September 08, 2019, 12:51:02 pm »
I am on FPC 3.0.4.
Code: Pascal  [Select]
  1. {$MODE DELPHI}
  2.  
  3. type
  4.   TOptValue<T:class> = record
  5.     class function SetResultValue(const Token: TOptValue<T>; const Tmp: T): Boolean; static;
  6.   end;
  7.  
  8. class function TOptValue<T>.SetResultValue(const Token: TOptValue<T>; const Tmp: T): Boolean;
  9. begin
  10.   Result := True;
  11. end;
  12.  
  13. var
  14.   V: TOptValue<TObject> = ();
  15. begin
  16.   V.SetResultValue(V, nil);
  17. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 8973
Re: Generic functions/methods in delphi mode
« Reply #4 on: September 08, 2019, 01:25:01 pm »
Nothing wrong with the demo code, since it works both in Delphi and FPC, but: I suppose he knew that and tried the stand-alone feature. That requires 3.2.0.

What is funny is that you should have written something like my {$ifdef} and I should have written something like your {$ifdef}  8-) :o
Anyway, what about adding both the examples to the wiki, with the proper {$ifdef} 's ?
« Last Edit: September 08, 2019, 01:41:25 pm by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

PascalDragon

  • Hero Member
  • *****
  • Posts: 639
  • Compiler Developer
Re: Generic functions/methods in delphi mode
« Reply #5 on: September 09, 2019, 11:36:00 am »
Note this code needs FPC 3.2.0 to work and unlike maybe suggested by the mode it DOES NOT WORK in Delphi!!! Delphi doesn't support it.
See: https://wiki.freepascal.org/FPC_New_Features_3.2#Support_for_generic_routines
Please note that the idea of the mode concept is that code written for that compiler works in FPC. We do in no way guarantee that the opposite is true (which is why I implemented support for global generic functions also for mode Delphi).