Recent

Author Topic: Generic procedure  (Read 1622 times)

Milsa

  • Sr. Member
  • ****
  • Posts: 328
Generic procedure
« on: June 14, 2020, 09:29:07 pm »
I want to rewrite this code to generic code:
https://wiki.freepascal.org/Array_sort
I have problem with this code:
Code: Pascal  [Select][+][-]
  1. unit SortGeneric;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. type
  11.   generic TCompareFunc<T> = function (const elem1, elem2: T): Integer;
  12.  
  13. generic procedure Sort<T>(var Arr: array of T; Count: Integer; CompareFunc: TCompareFunc);
  14.  
  15. implementation
  16.  
  17.  
  18. end.
  19.  
I have message from compiler:
Quote
sortgeneric.pas(13,9) Fatal: Syntax error, "identifier" expected but "PROCEDURE" found
My source code is good because this code is in this post:
https://forum.lazarus.freepascal.org/index.php/topic,33327.msg215780.html#msg215780
But it does not work. Why?
I work with Lazarus 4.0, FPC 3.2.2, date 2025-05-03
This information is actual to: 3rd Aug 2025

MarkMLl

  • Hero Member
  • *****
  • Posts: 8525
Re: Generic procedure
« Reply #1 on: June 14, 2020, 10:04:03 pm »
What version of compiler?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Milsa

  • Sr. Member
  • ****
  • Posts: 328
Re: Generic procedure
« Reply #2 on: June 14, 2020, 10:23:57 pm »
Lazarus 2.0.8 FPC 3.0.4.
I work with Lazarus 4.0, FPC 3.2.2, date 2025-05-03
This information is actual to: 3rd Aug 2025

MarkMLl

  • Hero Member
  • *****
  • Posts: 8525
Re: Generic procedure
« Reply #3 on: June 14, 2020, 10:44:36 pm »
The thread you cite EXPLICITLY says that it needs later than 3.0.x. Try trunk or the 3.2 release candidate.

Also https://forum.lazarus.freepascal.org/index.php/topic,50024.msg364222.html#msg364222

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Milsa

  • Sr. Member
  • ****
  • Posts: 328
Re: Generic procedure
« Reply #4 on: June 14, 2020, 10:45:37 pm »
Solution with 3.0.4 is only with generic class?
I work with Lazarus 4.0, FPC 3.2.2, date 2025-05-03
This information is actual to: 3rd Aug 2025

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Generic procedure
« Reply #5 on: June 14, 2020, 11:21:46 pm »
The way you have written it, the T in TCompareFunc<T> is completely unrelated to the T in Sort<T>.
So, irrespective of compiler version, these two unrelated routines cannot work together.
I think you must wrap them in a class or record so there is only a single T known by the two routines.

bytebites

  • Hero Member
  • *****
  • Posts: 776
Re: Generic procedure
« Reply #6 on: June 15, 2020, 04:55:01 am »
OP's code does not compile (with trunk), since specializate is missing.

Code: Pascal  [Select][+][-]
  1. unit SortGeneric;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. type
  11.   generic TCompareFunc<T> = function (const elem1, elem2: T): Integer;
  12.  
  13. generic procedure Sort<T>(var Arr: array of T; Count: Integer; CompareFunc:specialize TCompareFunc<T>);
  14.  
  15. implementation
  16.  
  17. generic procedure Sort<T>(var Arr: array of T; Count: Integer; CompareFunc:specialize TCompareFunc<T>);
  18. begin
  19. end;
  20.  
  21. end.

 

TinyPortal © 2005-2018