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
TMyCustomAttr = class(TCustomAttribute)
public
constructor Create(const AView: TClass); overload;
end;
TMyGenericClass
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
/sub. I'm interested in the answer...
ReplyDeleteHm, I would try to create a generic attribute with a parameterless constructor: TMyCustomAttribute = class(TCustomAttribute) end;
ReplyDeleteThe usage would look like this:
[TMyCustomAttr]
procedure Test;
I didn't test it so far!
Christopher Wosinski Yes, that should work.
ReplyDeleteI could not do a full test, but the compiler returns a warning!
ReplyDeleteTMyGenericClass = class(TInterfacedObject, IInvokable)
public
[TMyCustomAttrGeneric] // ~> W1025 Unsupported language feature: 'custom attribute'
procedure Test;
end;
TMyCustomAttrGeneric = class(TCustomAttribute)
public
function GetClassName: String;
end;
Christopher Wosinski That gives: W1025 Unsupported language feature: 'custom attribute'
ReplyDeleteEurides 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.
David Heffernan So referring to this http://stackoverflow.com/questions/25746629/vcl-printers-pas888-w1025-unsupported-language-feature-custom-attribute
ReplyDeleteThe 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
Thanks for the answers! I expected that the solution was more elegant, sorry!
ReplyDeleteDavid Heffernan You added nothing to the thread (yet again), so why the comment?
ReplyDeleteThe 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?
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.
ReplyDeleteDavid Heffernan "So personally, I for one have no intention to invest time in trying to help because any knowledge will not be captured." - Awesome...
ReplyDeleteLars Fosdal you'd rather any insight was lost?
ReplyDeleteI 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.
ReplyDeleteMy goal is to create a place for questions and discussions, not to create the optimal knowledge base for posterity.
Participation is voluntary.
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.
ReplyDeleteWhat I don't appreciate is that the obnoxious response from Nicholas Ring is tolerated.
David Heffernan You didn't suggest any about posting to SO. You said what your personal preference is.
ReplyDeleteAs to my obnoxious response, I just voiced my personal opinion... As you did.
Nicholas Ring You are expected not to be so rude and obnoxious. I'm going to ignore you from now on.
ReplyDeleteDavid 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.
ReplyDeletePersonally 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"?
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.
ReplyDeleteYes, 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.
ReplyDeleteLars Fosdal But Nicholas is fine to be personally abusive?
ReplyDeleteIf he was personally abusive, David Heffernan, it seems to have been removed?
ReplyDeleteLars Fosdal No, still there, his first comment.
ReplyDeleteI see a pointed opinion about your contribution, but no personal abuse.
ReplyDeleteLars 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.
ReplyDeleteSince 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?
ReplyDeleteLars Fosdal Because the asker might not realise all of those facts.
ReplyDeleteSo, if I add in the community description, that this is not SO and comments can't be searched - we could stop reminding the asker?
ReplyDeleteLars Fosdal I think I'd still try to help people as best as possible
ReplyDeleteThat is appreciated. Note that I did add notes about questions and searching to the community description to reduce the need for reminders.
ReplyDelete