Array indexed by generic enum ? lazarus / 62-General ---------------------------------------------------------------------- guest58172 · 2018-03-11 20:39 I feel a bit blocked because this is not accepted Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  generic TFoo = class    a: array[SomeEnum] of integer;  end;   TEnum = (e0, e1); var   a: specialize TFoo;  Is this supported in a way or another ? ---------------------------------------------------------------------- bytebites · 2018-03-11 21:11 a: array[Low(SomeEnum)..High(SomeEnum)] of integer; ---------------------------------------------------------------------- guest58172 · 2018-03-11 21:15 This is what i'm trying to do in a helper record: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   generic TEnumIndexedArray = record  private    fArray: array[Low(TheEnum) .. High(TheEnum)] of ElementType;    procedure setElement(index: TheEnum; value: ElementType);    function getElement(index: TheEnum): ElementType;  public    property element[index: TheEnum]: ElementType read getElement write setElement; default;  end;  procedure TEnumIndexedArray.setElement(index: TheEnum; value: ElementType);begin  fArray[integer(index)] := value;end; function TEnumIndexedArray.getElement(index: TheEnum): ElementType;begin  result := fArray[integer(index)];end;  but the compiler says: Error: Type mismatch ---------------------------------------------------------------------- guest58172 · 2018-03-11 21:33 I can use Byte boundaries but this not super nice, not strongly typed. At least the ugly things are hidden. ---------------------------------------------------------------------- bytebites · 2018-03-12 03:12 It works in trunk-version. ---------------------------------------------------------------------- ASerge · 2018-03-12 18:02 Quote from: BBasile on March 11, 2018, 09:39:41 pmIs this supported in a way or another ? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;{$MODE OBJFPC}{$APPTYPE CONSOLE}{$MODESWITCH ADVANCEDRECORDS} uses TypInfo; type  generic TEnumArray = record  private    FArray: array of TData;    FMinValue: LongInt;  function GetItem(const Index: TEnum): TData; inline;    procedure SetItem(const Index: TEnum; const Value: TData); inline;  public    property Items[Index: TEnum]: TData read GetItem write SetItem; default;    procedure Init;  end; function TEnumArray.GetItem(const Index: TEnum): TData;begin  Result := FArray[LongInt(Index) - FMinValue];end; procedure TEnumArray.Init;var  EnumTypeInfo: PTypeInfo;  EnumTypeData: PTypeData;  i: LongInt;begin  EnumTypeInfo := TypeInfo(TEnum);  assert(EnumTypeInfo^.Kind = tkEnumeration, 'Support only enum types');  EnumTypeData := GetTypeData(EnumTypeInfo);  FMinValue := EnumTypeData^.MinValue;  SetLength(FArray, EnumTypeData^.MaxValue - FMinValue + 1);  for i := Low(FArray) to High(FArray) do    FArray[i] := Default(TData);end; procedure TEnumArray.SetItem(const Index: TEnum; const Value: TData);begin  FArray[LongInt(Index) - FMinValue] := Value;end; type  TOneTwoThree = (One, Two, Three);  TTwoThree = Two..Three; var  X: specialize TEnumArray;begin  X.Init;  X[Two] := One;  X[Three] := X[Two];  Writeln(X[Three]);  Readln;end. ---------------------------------------------------------------------- Thaddy · 2018-03-20 07:44 In trunk it is even easier since last week, mantis 33414: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+}uses Classes, SysUtils; type  generic TFoo = class    m: array[SomeEnum] of integer;  end;   TEnum = (e0, e1); var  a: specialize TFoo; beginend. But it needs trunk revision 38532 or higher. No need for typinfo. It doesn't need a class. It also works for TArray Cleaned up code in proper syntax: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode delphi} type  TFoo = class    m: array[SomeEnum] of integer;  end;  TEnum = (e0, e1);var  a: TFoo;beginend. ---------------------------------------------------------------------- Thaddy · 2018-03-20 11:29 Quote from: ASerge on March 12, 2018, 07:02:19 pm Cumbersome and slow. At least trunk works now... ---------------------------------------------------------------------- ASerge · 2018-03-21 00:39 Quote from: Thaddy on March 20, 2018, 12:29:32 pmCumbersome and slow. At least trunk works now... Yes, but it works!