Forum > General

Type helper for TArray<T>

(1/1)

JdeHaan:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit uArray; {$mode delphi}{$H+}{$ModeSwitch typehelpers} interface uses  SysUtils; type   TArray<T> = array of T;   TArrayHelper<T> = type helper for TArray<T>    procedure InsertRange(const Index: Integer; Values: TArray<T>);  end; implementation { TArrayHelper } procedure TArrayHelper<T>.InsertRange(const Index: Integer; Values: TArray<T>);begin  // insert the rangeend; end. {4 errors on line 15 Error: Syntax error "indentifier expected, but "<" foundError: Type identifier expectedError: Generics without specialization cannot be used as a type for a variable [15,44] Error: expected ., but T found} 

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

Thaddy:
TArray<T> is already in system, so that may clash with a re-declaration.
E.g. this small example unit:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit naamloosunit;{$mode delphi}interfacetype   TspecializeMeToInteger = TArray<Integer>;implementationend.

PascalDragon:

--- Quote from: JdeHaan on July 03, 2022, 12:04:22 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.
--- End quote ---

Generic type helpers are currently not supported.


--- Quote from: Thaddy on July 03, 2022, 02:10:09 pm ---TArray<T> is already in system, so that may clash with a re-declaration.
--- End quote ---

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:

--- Quote --- Generic type helpers are currently not supported.

--- End quote ---

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

PascalDragon:

--- Quote from: JdeHaan on July 03, 2022, 02:47:51 pm ---
--- Quote --- Generic type helpers are currently not supported.

--- End quote ---

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

--- End quote ---

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.

Navigation

[0] Message Index

Go to full version