Recent

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

MountainQ

  • Jr. Member
  • **
  • Posts: 65
[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: 14205
  • Probably until I exterminate Putin.
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 »
Specialize a type, not a var.

MountainQ

  • Jr. Member
  • **
  • Posts: 65
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: 5446
  • 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: 14205
  • Probably until I exterminate Putin.
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..
Specialize a type, not a var.

MountainQ

  • Jr. Member
  • **
  • Posts: 65
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.

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: [Closed] generics and helpers
« Reply #6 on: December 23, 2023, 10:11:31 pm »
@PascalDragon did you ever get to implement type helpers for generics? My current tests shows that it is not possible. Is there an issue to follow?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: [Closed] generics and helpers
« Reply #7 on: December 27, 2023, 06:48:37 pm »
@PascalDragon did you ever get to implement type helpers for generics? My current tests shows that it is not possible. Is there an issue to follow?

No and not that I'm aware of.

Okoba

  • Hero Member
  • *****
  • Posts: 528
Re: [Closed] generics and helpers
« Reply #8 on: January 04, 2024, 07:41:53 am »

 

TinyPortal © 2005-2018