What is the solution to case below? (E2026 Constant expression expected)

What is the solution to case below? (E2026 Constant expression expected)

TMyCustomAttr = class(TCustomAttribute)
public
constructor Create(const AView: TClass); overload;
end;

TMyGenericClass = class(TInterfacedObject, IInvokable)
public
[TMyCustomAttr(TView)] // ~> error: E2026 Constant expression expected
//[TMyCustomAttr(TForm)]
procedure Test;
end;

My generic class (TMyGenericClass) is annotated with Custom Attribute (TMyCustomAttr), I would like to send to my TMyCustomAttr the generic value and not set a fix value (for example TForm)...
Is evident in documentation: "You Can Only Use Constant Expressions the Attribute Parameters"
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Annotating_Types_and_Type_Members#Allowed_Parameters

What are the alternatives?
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Annotating_Types_and_Type_Members#Allowed_Parameters

Comments

  1. /sub. I'm interested in the answer...

    ReplyDelete
  2. Hm, I would try to create a generic attribute with a parameterless constructor: TMyCustomAttribute = class(TCustomAttribute) end;

    The usage would look like this:
    [TMyCustomAttr]
    procedure Test;

    I didn't test it so far!

    ReplyDelete
  3. Christopher Wosinski Yes, that should work.

    ReplyDelete
  4. I could not do a full test, but the compiler returns a warning!

    TMyGenericClass = class(TInterfacedObject, IInvokable)
    public
    [TMyCustomAttrGeneric] // ~> W1025 Unsupported language feature: 'custom attribute'
    procedure Test;
    end;

    TMyCustomAttrGeneric = class(TCustomAttribute)
    public
    function GetClassName: String;
    end;

    ReplyDelete
  5. Christopher Wosinski That gives: W1025 Unsupported language feature: 'custom attribute'

    Eurides Baptistella Whilst this is very interesting, any clever ideas that you get will be lost forever because this group is pretty much unsearchable. So personally, I for one have no intention to invest time in trying to help because any knowledge will not be captured. This is why I far prefer SO.

    ReplyDelete
  6. David Heffernan So referring to this http://stackoverflow.com/questions/25746629/vcl-printers-pas888-w1025-unsupported-language-feature-custom-attribute
    The Delphi compiler can't find an attribute with the given name. Right?

    Eurides Baptistella The only ugly workaround I get in mind is to pass the full qualified class name as string to the attribute and load the class type on runtime using RTTI: http://stackoverflow.com/questions/5772755/delphi-create-class-from-a-string

    ReplyDelete
  7. Thanks for the answers! I expected that the solution was more elegant, sorry!

    ReplyDelete
  8. David Heffernan You added nothing to the thread (yet again), so why the comment?
    The community is about "Be constructive, informative and helpful." to which your comment it not. If you aren't going to be constructive, informative or helpful, then why are you in this group?

    ReplyDelete
  9. Nicholas Ring I consider your comment to be rude and destructive and will ignore it. You don't like me. Fine. Do feel free to ignore me.

    ReplyDelete
  10. David Heffernan "So personally, I for one have no intention to invest time in trying to help because any knowledge will not be captured." - Awesome...

    ReplyDelete
  11. Lars Fosdal​ you'd rather any insight was lost?

    ReplyDelete
  12. I agree that community search is totally broken, but if someone asks a question - it wouldn't be much of a community if it's members declined to offer their knowledge, experience or opinions - because the knowledge would not be captured, or the question was not well written.

    My goal is to create a place for questions and discussions, not to create the optimal knowledge base for posterity.

    Participation is voluntary.

    ReplyDelete
  13. Lars Fosdal  That's fine. I was explaining why I personally don't want to invest effort into answering detailed questions here, and suggesting that SO would elicit more response.

    What I don't appreciate is that the obnoxious response from Nicholas Ring is tolerated.

    ReplyDelete
  14. David Heffernan​ You didn't suggest any about posting to SO. You said what your personal preference is.
    As to my obnoxious response, I just voiced my personal opinion... As you did.

    ReplyDelete
  15. Nicholas Ring You are expected not to be so rude and obnoxious. I'm going to ignore you from now on.

    ReplyDelete
  16. David Heffernan - I've been online for as long as it has been possible. Everywhere there are and have been people that join forums and state "come to our place for help", instead of contributing in the forum.

    Personally and generally speaking, I think that is rude and obnoxious - but SO is a good resource, so I won't scorn you for it.

    Since SO doesn't do so well with discussions (and poorly formed questions), I guess these two places complement each other, but I wonder what would happen if people started commenting "come to forum so and so to discuss this"?

    ReplyDelete
  17. Lars Fosdal Oh come on, I contribute plenty here. I just stated that a detailed question like the one asked here would be better suited on SO. There are plenty of discussions and topics that are better suited to this group. SO and this group are complementary.

    ReplyDelete
  18. Yes, you do definitively contribute, but your rather direct commenting style can at times come across as obnoxious. I do my best to ignore it - but sometimes it gets to me as well.

    ReplyDelete
  19. Lars Fosdal But Nicholas is fine to be personally abusive?

    ReplyDelete
  20. If he was personally abusive, David Heffernan, it seems to have been removed?

    ReplyDelete
  21. Lars Fosdal No, still there, his first comment.

    ReplyDelete
  22. I see a pointed opinion about your contribution, but no personal abuse.

    ReplyDelete
  23. Lars Fosdal  Well, if you think that sort of comment is fine then you have a totally different stance on what is constructive than I do. I wasn't impressed with your sarcastic comment earlier either. Constructive sarcasm? That's an oxymoron.

    ReplyDelete
  24. Since we have established the fact that this is not SO, and it is desirable, but not mandatory to ask perfect questions, and it's an acknowledged fact that comments written here, currently are not searchable - why do we have these discussions again and again?

    ReplyDelete
  25. Lars Fosdal  Because the asker might not realise all of those facts.

    ReplyDelete
  26. So, if I add in the community description, that this is not SO and comments can't be searched - we could stop reminding the asker?

    ReplyDelete
  27. Lars Fosdal​ I think I'd still try to help people as best as possible

    ReplyDelete
  28. That is appreciated. Note that I did add notes about questions and searching to the community description to reduce the need for reminders.

    ReplyDelete

Post a Comment