1

Is it possible to use an open array as index type for indexed properties?

unit OpenArrayAsPropertyIndex;

interface

type

  TFoo = class
  private
    function getBar(Index: array of Integer): String;
  public
    // [DCC Error]: E2008 Incompatible types
    property Bar[Index: array of Integer]: String read getBar;
  end;

implementation

function TFoo.getBar(Index: array of Integer): String;
begin

end;

end.

The property getter is generated by pressing Ctl+Shift+C in the IDE, but this code doesn't compile and gives error "E2008 Incompatible types". So, is it a language limitation, or what is the correct parameter signature for the getter?

5
  • I don't think it matters, but try adding const. Commented Dec 15, 2021 at 13:10
  • I.e. getBar(const Index: array of Integer). Commented Dec 15, 2021 at 13:11
  • Counter question: how should the code look to use such a property? Do you have an answer for this? Commented Dec 15, 2021 at 13:44
  • @Uli Gerhardt: "const" doesn't work, it gives the same error. Commented Dec 15, 2021 at 13:51
  • @AmigoJack: I'm just playing. But obviously it should be used as "s := Foo.Bar[[1,2,3]];" Commented Dec 15, 2021 at 13:57

1 Answer 1

1

Instead of array of Integer use TIntegerDynArray from System.Types or a similar self declared type.

type
  TIntArray = array of Integer;
  TFoo = class
  private
    function getBar(Index: TIntArray): String;
  public
    property Bar[Index: TIntArray]: String read getBar;
  end;

function TFoo.getBar(Index: TIntArray): String;
begin

end;
Sign up to request clarification or add additional context in comments.

3 Comments

TArray<Integer>
But this is a dynamic array, not an open array. (Which might be fine for the OP, considering his comment above.)
Open arrays are only allowed as parameters of procedures et al. Most likely that doesn't include indexed properties. It depends on the intended use if my answer is valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.