This form of generics is not available in FPC 3.0.4
I tried:You should declare it like this:
generic procedure printTArr<T>(sarr: T); var item: string; // BUT WHAT TO PUT HERE INSTEAD OF STRING? begin for item in sarr do write(item, ','); writeln; end;
Is there any way to have generic function here that will print any type of printable item array sent to it?In FPC 3.0 only via generic types:
Is it possible to have simple generic functions?
Yes, this works. But this means for generics, full class has to be defined and it cannot be done with just a generic function.In the new version of FPC it is possible.
In the new version of FPC it is possible.
3.0.0. is several years old. So no wonder it does not work as expected.
This feature works in 3.0.4 and higher.
unit1.pas(23,1) Fatal: Syntax error, "BEGIN" expected but "identifier GENERIC" found
You don't need to declare a class, you can also use a record instead (requires {$modeswitch advancedrecords} in mode ObjFPC).Is there any way to have generic function here that will print any type of printable item array sent to it?In FPC 3.0 only via generic types:
Is it possible to have simple generic functions?
{$APPTYPE CONSOLE} {$MODE DELPHI} type TPrintArray<T> = class(TObject) public class procedure DoPrint(const A: array of T); static; end; class procedure TPrintArray<T>.DoPrint(const A: array of T); var Item: T; begin for Item in A do Write(Item, ','); Writeln; end; begin TPrintArray<Integer>.DoPrint([1, 2, 3]); TPrintArray<string>.DoPrint(['11', '22', '33']); Readln; end.