-
-
Notifications
You must be signed in to change notification settings - Fork 683
UDAs for function arguments #7576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| UDAs on function arguments are now supported | ||
|
|
||
| User-defined attributes on function arguments behave analogous to existing UDAs: | ||
|
|
||
| --- | ||
| void test(A)(@(22) A a) | ||
| { | ||
| static assert([__traits(getAttributes, a)] == [22]); | ||
| } | ||
| --- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1734,15 +1734,17 @@ struct ASTBase | |
| Type type; | ||
| Identifier ident; | ||
| Expression defaultArg; | ||
| UserAttributeDeclaration userAttribDecl; // user defined attributes | ||
|
|
||
| extern (D) alias ForeachDg = int delegate(size_t idx, Parameter param); | ||
|
|
||
| final extern (D) this(StorageClass storageClass, Type type, Identifier ident, Expression defaultArg) | ||
| final extern (D) this(StorageClass storageClass, Type type, Identifier ident, Expression defaultArg, UserAttributeDeclaration userAttribDecl) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expression defaultArg, UserAttributeDeclaration userAttribDecl = null)Giving this a default value would save on a lot of code edits that just add a null as the last parameter. Not sure if default values are being avoided here on purpose or not.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT is the prevalent coding style in the DMD source code to prefer explicitness. |
||
| { | ||
| this.storageClass = storageClass; | ||
| this.type = type; | ||
| this.ident = ident; | ||
| this.defaultArg = defaultArg; | ||
| this.userAttribDecl = userAttribDecl; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RazvanN7 do you have any plans on removing the giant code duplication in |
||
| } | ||
|
|
||
| static size_t dim(Parameters* parameters) | ||
|
|
@@ -1809,7 +1811,7 @@ struct ASTBase | |
|
|
||
| Parameter syntaxCopy() | ||
| { | ||
| return new Parameter(storageClass, type ? type.syntaxCopy() : null, ident, defaultArg ? defaultArg.syntaxCopy() : null); | ||
| return new Parameter(storageClass, type ? type.syntaxCopy() : null, ident, defaultArg ? defaultArg.syntaxCopy() : null, userAttribDecl ? cast(UserAttributeDeclaration) userAttribDecl.syntaxCopy(null) : null); | ||
| } | ||
|
|
||
| void accept(Visitor v) | ||
|
|
@@ -3623,7 +3625,7 @@ struct ASTBase | |
| Expression e = (*exps)[i]; | ||
| if (e.type.ty == Ttuple) | ||
| e.error("cannot form tuple of tuples"); | ||
| auto arg = new Parameter(STC.undefined_, e.type, null, null); | ||
| auto arg = new Parameter(STC.undefined_, e.type, null, null, null); | ||
| (*arguments)[i] = arg; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps include an example of how to get the UDAs of a parameter from outside of a function. Or should that be left as an exercise for the reader 😃.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a bit more complicated and I think we will provide an interface in
std.traitsanyhow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.