Recent

Author Topic: [Closed] generics and helpers  (Read 270 times)

MountainQ

  • New Member
  • *
  • Posts: 46
[Closed] generics and helpers
« on: September 19, 2019, 08:06:48 am »
Greetings everyone,
as the title suggest, I was wondering whether it is possible to create helper classes based on a generic type, something like.
Code: Pascal  [Select]
  1. generic TArrayHelper<T> = type helper for array of T
  2.   // mighty procedures
  3. end;  
Alternatively, is it possible to use typecasting for the same purpose
Code: Pascal  [Select]
  1. generic TSortArray<T> = array of T // or specialize(TArray<T>)
  2.   procedure mysort();
  3. end;
  4.  
  5. ...
  6. var
  7.   arr: TStringArray;
  8. begin
  9.   ...
  10.   TSortArray(arr).mysort();
  11. end;
  12.  
Am I missing something? I did not find anything useful.
Best
« Last Edit: September 19, 2019, 10:15:06 am by MountainQ »

Thaddy

  • Hero Member
  • *****
  • Posts: 9278
Re: generics and helpers
« Reply #1 on: September 19, 2019, 08:30:07 am »
Both are not possible. (And the second one will never be possible: you can not do that in any language)
The first one can be implemented, possibly, but the second one is complete nonsense: because TSortArray needs to be specialized first to be able to have a valid cast type.
You can never hard cast to an unknown type. A hard cast requires a known type. (pointer types are a rare exception, but in this case also never possible)
« Last Edit: September 19, 2019, 08:39:42 am by Thaddy »
also related to equus asinus.

MountainQ

  • New Member
  • *
  • Posts: 46
Re: generics and helpers
« Reply #2 on: September 19, 2019, 08:51:56 am »
Thanks Thaddy for the quick reply; a quick follow-up for my understanding:
Quote
The first one can be implemented, possibly..
Does that mean a future version of FPC could do that?
Quote
.. A hard cast requires a known type..
That was poor writing on my part; I had something like that in mind (as already seen in the 1st post, yet commented out)
Code: Pascal  [Select]
  1. generic TSortArray<T> = specialize(TArray<T>)
  2.   procedure mysort();
  3. end;
This should make much more sense; is something like this possible?
I hope I do not overstretch your patience.


PascalDragon

  • Hero Member
  • *****
  • Posts: 705
  • Compiler Developer
Re: generics and helpers
« Reply #3 on: September 19, 2019, 09:03:02 am »
Quote
The first one can be implemented, possibly..
Does that mean a future version of FPC could do that?
It's on my ToDo list to implement it. However it would always require a specialization of the helper so that it would become usable.

Thaddy

  • Hero Member
  • *****
  • Posts: 9278
Re: generics and helpers
« Reply #4 on: September 19, 2019, 09:54:17 am »
Well, you've heard it from the Guru... :D Tnx Sven..
also related to equus asinus.

MountainQ

  • New Member
  • *
  • Posts: 46
Re: generics and helpers
« Reply #5 on: September 19, 2019, 10:14:53 am »
Thanks for your replies.
And thanks for your efforts PascalDragon
I close this thread.