Skip to content

Attributes with array of string constructor don't compile

Summary

It is not possible to use an attribute with constructor Create(Values: array of string);.

System Information

  • Operating system: Windows
  • Processor architecture: x86
  • Compiler version: trunk
  • Device: Computer

Example Project

program EnumAttrTest;

{$mode objfpc}{$H+}
{$modeswitch prefixedattributes}

uses
  SysUtils;

type
  EnumValues = class(TCustomAttribute)
  protected
    FValues: TStringArray;
  public
    constructor Create(Values: array of string);
  end;

  [EnumValues(['YES', 'NO', 'COULD_BE'])] // EnumAttrTest.lpr(17,16) Error: Ordinal expression expected
  TMyEnum = (meYes, meNo, meCouldBe);

const
  cValues2: array[0..2] of string = ('YES', 'NO', 'COULD_BE');
type
  [EnumValues(cValues2)] // EnumAttrTest.lpr(23,23) Error: Constant Expression expected
  TMyEnum2 = (me2Yes, me2No, me2CouldBe);

{ EnumValues }

constructor EnumValues.Create(Values: array of string);
begin
  FValues := Copy(Values, 0);
end;

begin
  EnumValues.Create(['YES', 'NO', 'COULD_BE']); // is OK
  EnumValues.Create(cValues2); // is OK
end.

What is the current bug behavior?

Compiler error when the attribute is used in the interface. When created as a normal class, everything is OK.

What is the expected (correct) behavior?

It should compile in the interface declaration as well.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information