Recent

Author Topic: Type helper for TArray<T>  (Read 526 times)

JdeHaan

  • Full Member
  • ***
  • Posts: 115
Type helper for TArray<T>
« on: July 03, 2022, 12:04:22 pm »
Hi,

I'm trying to extend TArray<T> generic with some functions and procedures, however, I can't get it to compile. Errors are in the code below. I'm not sure if it's possible what I want: to extend a generic with a generic type helper, or that I'm just messing it up. Pls give me some direction.

Code: Pascal  [Select][+][-]
  1. unit uArray;
  2.  
  3. {$mode delphi}{$H+}
  4. {$ModeSwitch typehelpers}
  5.  
  6. interface
  7.  
  8. uses
  9.   SysUtils;
  10.  
  11. type
  12.  
  13.   TArray<T> = array of T;
  14.  
  15.   TArrayHelper<T> = type helper for TArray<T>
  16.     procedure InsertRange(const Index: Integer; Values: TArray<T>);
  17.   end;
  18.  
  19. implementation
  20.  
  21. { TArrayHelper }
  22.  
  23. procedure TArrayHelper<T>.InsertRange(const Index: Integer; Values: TArray<T>);
  24. begin
  25.   // insert the range
  26. end;
  27.  
  28. end.
  29.  
  30. {
  31. 4 errors on line 15
  32.  
  33. Error: Syntax error "indentifier expected, but "<" found
  34. Error: Type identifier expected
  35. Error: Generics without specialization cannot be used as a type for a variable
  36.  
  37. [15,44] Error: expected ., but T found
  38. }
  39.  


Lazarus v 2.3.0 (date 2022-05-27); FPC v 3.3.1 (x86-64 Darwin Cocoa)

Thaddy

  • Hero Member
  • *****
  • Posts: 14198
  • Probably until I exterminate Putin.
Re: Type helper for TArray<T>
« Reply #1 on: July 03, 2022, 02:10:09 pm »
TArray<T> is already in system, so that may clash with a re-declaration.
E.g. this small example unit:
Code: Pascal  [Select][+][-]
  1. unit naamloosunit;
  2. {$mode delphi}
  3. interface
  4. type
  5.    TspecializeMeToInteger = TArray<Integer>;
  6. implementation
  7. end.
« Last Edit: July 03, 2022, 02:15:00 pm by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Type helper for TArray<T>
« Reply #2 on: July 03, 2022, 02:14:05 pm »
I'm trying to extend TArray<T> generic with some functions and procedures, however, I can't get it to compile. Errors are in the code below. I'm not sure if it's possible what I want: to extend a generic with a generic type helper, or that I'm just messing it up. Pls give me some direction.

Generic type helpers are currently not supported.

TArray<T> is already in system, so that may clash with a re-declaration.

Redeclarations are never a problem, units are namespaces after all. Having both units in the same scope however might lead to confusion (though dynamic arrays are handled a bit special here).

JdeHaan

  • Full Member
  • ***
  • Posts: 115
Re: Type helper for TArray<T>
« Reply #3 on: July 03, 2022, 02:47:51 pm »
Quote
Generic type helpers are currently not supported.

@PascalDragon, thanks. It would be a nice feature...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Type helper for TArray<T>
« Reply #4 on: July 04, 2022, 01:55:11 pm »
Quote
Generic type helpers are currently not supported.

@PascalDragon, thanks. It would be a nice feature...

Support for generic type helpers is on my ToDo list, but even if they're implemented you would still need to specialize the type helper for your final type, because the compiler can't do implicit specializations for types.

 

TinyPortal © 2005-2018