TCustomAttribute and class internal types

TCustomAttribute and class internal types

Consider

Default = class(TCustomAttribute)

  DefaultVal: Integer;

  constructor Create(const aVal:Integer);

end;

TMyBaseClass = class(TObject)

public

   constructor Create; virtual;  // handles attributes

end;

// 1

TMyClass = class(TMyBaseClass)

public

  [Default(5)]

  Value: Integer;

end;

which is ok, versus

TMyBaseClass = class(TObject)

public

   type

      Default = class(TCustomAttribute)

        DefaultVal: Integer;

        constructor Create(const aVal:Integer);

      end;

   constructor Create; virtual;  // handles attributes

end;

// 2

TMyClass = class(TMyBaseClass)

public

  [Default(5)]  

W1025 Unsupported language feature: ‘custom attribute’

  Value: Integer;

end;

as well as

// 3

TMyClass = class(TMyBaseClass)

public

  [TMyBaseClass.Default(5)]  

E2010 Incompatible types: ‘TQuerySet’ and ‘TCustomAttribute’

  Value: Integer;

end;

Question

I can understand why //2 is a fail, since internal types have to be qualified, but why is //3 not a compatible type?

Update: Feature Request added to QP

https://quality.embarcadero.com/browse/RSP-13512

9 thoughts on “TCustomAttribute and class internal types


  1. I guess the compiler is not able to resolve that qualified attribute name. It may have to do with the ability to accept [MyDefault(5)] even if the attribute class is named MyDefaultAttribute.


  2. David Heffernan – An example name of “Default” was a bad choice, yeah – as it clashes with Default(T); My bad.


    (In reality, it had a different name)


    Uwe Raabe – I had forgotten about that attribute type name magic. 


    Still, very annoying that you can’t use class internal types as these attributes are often class specific, and you are only able to think of so many variations on a “default” name.  Prefixes to the rescue – but – brevity would be nice too…


  3. Lars Fosdal I was not commenting on Default. By convention attribute class names are of the form


        FooAttribute


    and you use them like this


        [FooAttribute(42)]


    or


        [Foo(42)]


    Both will work. This allows you to include the word Attribute in the type name so that it is clear what it is when you define the type. But at the point of use when it is already obvious that this is an attribute, that word can be omitted.


  4. I wish Delphi would support instance field initialization just like you can do with global variables. Then you would not need this (I assume you are using this to set default values for your fields via attribute similar to the Spring4D TManagedObject)

Leave a Reply