Recent

Author Topic: generic Class for any Type ?  (Read 1702 times)

paule32

  • Hero Member
  • *****
  • Posts: 647
  • One in all. But, not all in one.
generic Class for any Type ?
« on: July 14, 2025, 02:22:21 pm »
I have a generic Class:

Code: Pascal  [Select][+][-]
  1. type
  2.   generic TStream<T1> = class(TObject)

I would have a function that use a Pointer or similar to this Class:

Code: Pascal  [Select][+][-]
  1. function  TStream_WriteBuffer(p: specialize TStream<T>; const Buffer; Count: Integer): Integer; stdcall; external RTLDLL;

But I fail with Error:
Error: Identifier not found "T"
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

gidesa

  • Sr. Member
  • ****
  • Posts: 260
Re: generic Class for any Type ?
« Reply #1 on: July 14, 2025, 02:46:41 pm »

Code: Pascal  [Select][+][-]
  1. function  TStream_WriteBuffer(p: specialize TStream<T>; const Buffer; Count: Integer): Integer; stdcall; external RTLDLL;

But I fail with Error:
Error: Identifier not found "T"

When you use the generic class, you have to instantiate the generic type parameter, that is T, with a concrete type.
For example with integer:
Code: Pascal  [Select][+][-]
  1. function  TStream_WriteBuffer(p: specialize TStream<Integer>; const Buffer; Count: Integer): Integer; stdcall; external RTLDLL;

Fibonacci

  • Hero Member
  • *****
  • Posts: 1045
  • Behold, I bring salvation - FPC Unleashed
Re: generic Class for any Type ?
« Reply #2 on: July 14, 2025, 02:50:20 pm »
Code: Pascal  [Select][+][-]
  1. generic function TStream_WriteBuffer<T>(p: specialize TStream<T>; const Buffer; Count: Integer): Integer; stdcall; external RTLDLL;
FPC Unleashed: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub

Khrys

  • Sr. Member
  • ****
  • Posts: 471
Re: generic Class for any Type ?
« Reply #3 on: July 14, 2025, 03:00:42 pm »
The compiler is right; there is no  "T"  in  TStream_WriteBuffer  because that function isn't generic. And you can't just change it to...

Code: Pascal  [Select][+][-]
  1. generic function TStream_WriteBuffer<T>(p: specialize TStream<T>; const Buffer; Count: Integer): Integer; stdcall; external RTLDLL;

...either, because DLLs don't contain generic templates, they contain machine code. Generics are a compile-time feature, so to provide a generic function in a DLL you'd have to specialize it for every type that could ever possibly be constructed in advance - which is impossible.

If you want to distribute generic functions, you must provide source code, period.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6403
  • Compiler Developer
Re: generic Class for any Type ?
« Reply #4 on: July 15, 2025, 09:53:18 pm »
If you want to distribute generic functions, you must provide source code, period.

Not quite. You can't put them in a DLL, but you can distribute them as binary by simply providing the PPU file of the unit which will be enough for the compiler (assuming the unit is from the same compiler version and for the same target with compatible settings).

Similarily dynamic packages will allow the same in the future, but for PPLs (which are libraries extended with special compiler magic functionality), but even there the compiler internally relies on the PPU files embedded in the corresponding PCP file at compiletime.

 

TinyPortal © 2005-2018