Recent

Author Topic: Type helper for generic type  (Read 1838 times)

elienaycarvalho

  • Newbie
  • Posts: 6
Type helper for generic type
« on: January 18, 2024, 10:44:43 pm »
Code: Pascal  [Select][+][-]
  1.  
  2. type vector<T> = record
  3. end;
  4.  
  5. type TVectorHelper = record helper for vector<string>
  6. end;
  7.  

Is possible?
« Last Edit: January 18, 2024, 10:48:39 pm by elienaycarvalho »

ASerge

  • Hero Member
  • *****
  • Posts: 2465
Re: Type helper for generic type
« Reply #1 on: January 18, 2024, 10:59:16 pm »
Is possible?
Code: Pascal  [Select][+][-]
  1. {$MODE DELPHI}
  2.  
  3. type TVector<T> = record
  4. end;
  5.  
  6. type TStringVector = TVector<string>;
  7.  
  8. type TVectorHelper = record helper for TStringVector
  9. end;
  10.  
  11. begin
  12. end.

simone

  • Hero Member
  • *****
  • Posts: 678
Re: Type helper for generic type
« Reply #2 on: January 18, 2024, 11:55:58 pm »
If you are not using Delphi mode, but ObjFPC mode, then you can write the following equivalent code:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$MODE OBJFPC}
  3. {$MODESWITCH ADVANCEDRECORDS}
  4.  
  5. type
  6.  
  7.   generic TVector<T> =record
  8.   end;
  9.  
  10.   TStringVector = specialize TVector<string>;
  11.  
  12.   TVectorHelper = record helper for TStringVector
  13.   end;
  14.  
  15. begin
  16. end.  

Post Scriptum: It is not necessary to repeat the keyword type for each definition.

« Last Edit: January 19, 2024, 12:03:27 am by simone »
Microsoft Windows 10/11 64 bit - Lazarus 3.8/4.0 FPC 3.2.2 x86_64-win64-win32/win64

elienaycarvalho

  • Newbie
  • Posts: 6
Re: Type helper for generic type
« Reply #3 on: January 19, 2024, 01:13:37 am »
Thanks...

Code: Pascal  [Select][+][-]
  1. type TVectorHelper = record helper for vector<string>
compiles at Delphi 11.3

about type keyword, is habit...

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: Type helper for generic type
« Reply #4 on: January 19, 2024, 01:29:21 am »
In this particular case it is possible, cos OP wants type helper for specific specialized generic type.

But, is it possible to actually make helper for generic type? AFAIK no.

It would be very useful, example:
You have String, WideString, and other strings eg UnicodeString. Currently you need to create multiple type helpers with the same exact code for all of them.

You could write one code for all String types and create specialized helper types, like:

Code: Pascal  [Select][+][-]
  1. type
  2.   generic StrHelper<T> = type helper for T
  3.     function padLeft(len: dword; padchar: char): T;
  4.     //...many more functions that would do the same for each type
  5.   end;
  6.  
  7.   StringHelper  = specialize StrHelper<String>;
  8.   WideHelper    = specialize StrHelper<WideString>;
  9.   UnicodeHelper = specialize StrHelper<UnicodeString>;
  10.  
  11. function StrHelper.padLeft(len: dword; padchar: char): T;
  12. begin
  13. end;

I have checked how its done in SysUtils in syshelp.inc:

Code: Pascal  [Select][+][-]
  1. {$define TStringHelper:=TAnsiStringHelper}
  2. {$i syshelps.inc}
  3.  
  4. {$define TStringHelper:=TWideStringHelper}
  5. {$i syshelps.inc}
  6.  
  7. {$define TStringHelper:=TUnicodeStringHelper}
  8. {$i syshelps.inc}

Clever, but generic helpers would help a lot.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6191
  • Compiler Developer
Re: Type helper for generic type
« Reply #5 on: January 21, 2024, 12:25:42 pm »
Code: Pascal  [Select][+][-]
  1.  
  2. type vector<T> = record
  3. end;
  4.  
  5. type TVectorHelper = record helper for vector<string>
  6. end;
  7.  

Is possible?

This is currently not possible, though that's mainly a weakness of the parser that needs to be fixed.

If you are not using Delphi mode, but ObjFPC mode, then you can write the following equivalent code:

You don't need mode ObjFPC for that.

But, is it possible to actually make helper for generic type? AFAIK no.

It would be very useful, example:
You have String, WideString, and other strings eg UnicodeString. Currently you need to create multiple type helpers with the same exact code for all of them.

It's on the ToDo list. In fact there's already a merge request for it for which I would need the time to get around to.

 

TinyPortal © 2005-2018