Given the following code snippet:
type
MyIntf = interface
['{C6184693-663E-419F-B2DA-4DA1A0E33417}']
procedure Foo;
end;
InvisiblePropInterfaces = class(TCustomAttribute)
private
FGUIDS: array of TGUID;
public
constructor Create(const GUIDS: array of TGUID);
end;
[InvisiblePropInterfaces([MyIntf])] // <-- Constant expression expected error
TMyClass = class(TInterfacedObject, MyIntf)
procedure Foo;
end;
Why does the compiler think this is not a constant expression ?
But given that I use InvisiblePropInterfaces like this, the compiler is just happy?
...
var
I: InvisiblePropInterfaces;
begin
I:= InvisiblePropInterfaces.Create([MyIntf]);
...
The pertinent section of the attributes documentation is this:
The key point is that a constant expression is a technical Pascal term that is not the same thing as a constant. I suspect that this is the root of the confusion.
Since it is not possible to have a constant expression that can be passed to a
TGUID, you are out of luck with your attribute. Indeed it is just as impossible to have a constant expression that can be passed to an open array parameter.I suppose that you could use the string representation of the
GUIDto solve the conundrum but that will leave you with messy duplication and an inability to pass arrays of GUIDs.