digitalmars.D.learn - Syntax for Static Import of User Define Attributes
- Vijay Nayar (45/45) Jul 27 2023 What is the correct syntax to use a static import of a
- Paul Backus (9/18) Jul 27 2023 You almost had it. The correct syntax is to enclose the entire
- Dennis (5/14) Jul 27 2023 Try:
- Vijay Nayar (4/19) Jul 28 2023 This one causes a different error, because it invokes the
- Steven Schveighoffer (5/25) Jul 28 2023 I tried it and it worked for me. The template-argument syntax is normal....
- Vijay Nayar (10/16) Jul 28 2023 You're right, I must have tried something slightly different
- Steven Schveighoffer (6/9) Jul 28 2023 It might be possible to expand the grammar. It seems very specific to
- Dennis (20/25) Jul 28 2023 Yes, parsing arbitrary expressions after an `@` would result in
- ryuukk_ (8/8) Jul 28 2023 Whenever there might be symbol clash, or when i want to make sure
What is the correct syntax to use a static import of a user-defined-attribute? Context: Attributes can have names that conflict with local variables. It is desired to use the fully-qualified-name of the attribute to get around this. Example: ``` import vibe.data.serialization : name, optional; class ChatCompletionFunctions { name("name") // Error: "name" refers to the string, not the attribute name. string name; name("max-tokens") int maxTokens; } ``` Attempted Fix 1: static imports ``` static import vibe.data.serialization; class ChatCompletionFunctions { vibe.data.serialization.name("name") ... } ``` This produces the error, it seems to be because the ` ` symbol stops capturing the annotation name at the first dot it encounters. ``` source/openai/model/ChatCompletionFunctions.d(19,32): Error: unexpected `(` in declarator ``` Attempted Fix 2: Enclose the entire attribute name in parenthesis. ``` static import vibe.data.serialization; class ChatCompletionFunctions { (vibe.data.serialization.name)("name") ... } ``` This simply gives: ``` source/openai/model/ChatCompletionFunctions.d(19,34): Error: declaration expected, not `(` ``` Is static importing of UDAs even supported in the language?
Jul 27 2023
On Thursday, 27 July 2023 at 21:19:08 UTC, Vijay Nayar wrote:Attempted Fix 2: Enclose the entire attribute name in parenthesis. ``` static import vibe.data.serialization; class ChatCompletionFunctions { (vibe.data.serialization.name)("name") ... } ```You almost had it. The correct syntax is to enclose the entire attribute (not just the name) in parentheses: ``` class ChatCompletionFunctions { (vibe.data.serialization.name("name")) string name; } ```
Jul 27 2023
On Thursday, 27 July 2023 at 21:19:08 UTC, Vijay Nayar wrote:Attempted Fix 2: Enclose the entire attribute name in parenthesis. ``` static import vibe.data.serialization; class ChatCompletionFunctions { (vibe.data.serialization.name)("name") ... } ```Try: ```D (vibe.data.serialization.name("name")) ```
Jul 27 2023
On Thursday, 27 July 2023 at 21:24:44 UTC, Dennis wrote:On Thursday, 27 July 2023 at 21:19:08 UTC, Vijay Nayar wrote:This one causes a different error, because it invokes the template-argument syntax: https://dlang.org/spec/attribute.html#udaAttempted Fix 2: Enclose the entire attribute name in parenthesis. ``` static import vibe.data.serialization; class ChatCompletionFunctions { (vibe.data.serialization.name)("name") ... } ```Try: ```D (vibe.data.serialization.name("name")) ```
Jul 28 2023
On 7/28/23 4:15 AM, Vijay Nayar wrote:On Thursday, 27 July 2023 at 21:24:44 UTC, Dennis wrote:I tried it and it worked for me. The template-argument syntax is normal. It's just a list of types or expressions (i.e. not a function argument list) What is the error? -SteveOn Thursday, 27 July 2023 at 21:19:08 UTC, Vijay Nayar wrote:This one causes a different error, because it invokes the template-argument syntax: https://dlang.org/spec/attribute.html#udaAttempted Fix 2: Enclose the entire attribute name in parenthesis. ``` static import vibe.data.serialization; class ChatCompletionFunctions { (vibe.data.serialization.name)("name") ... } ```Try: ```D (vibe.data.serialization.name("name")) ```
Jul 28 2023
On Friday, 28 July 2023 at 11:54:12 UTC, Steven Schveighoffer wrote:On 7/28/23 4:15 AM, Vijay Nayar wrote: I tried it and it worked for me. The template-argument syntax is normal. It's just a list of types or expressions (i.e. not a function argument list) What is the error? -SteveYou're right, I must have tried something slightly different without realizing it. The syntax that works seems to be to enclose the entire fully-qualified-name of the user-defined-attribute along with all its arguments within parentheses. However, this makes me wonder. Is there any reason why the ` ` shouldn't recognize the dots in a fully-qualified-name on its own, without the need for parentheses?
Jul 28 2023
On 7/28/23 8:10 AM, Vijay Nayar wrote:However, this makes me wonder. Is there any reason why the ` ` shouldn't recognize the dots in a fully-qualified-name on its own, without the need for parentheses?It might be possible to expand the grammar. It seems very specific to UDAs, as it doesn't just throw out `Expression` or whatnot. It probably has to do with the spot that it's in (declaration). I'll defer to the compiler experts to decide what makes the most sense. -Steve
Jul 28 2023
On Friday, 28 July 2023 at 12:20:05 UTC, Steven Schveighoffer wrote:On 7/28/23 8:10 AM, Vijay Nayar wrote: It might be possible to expand the grammar. It seems very specific to UDAs, as it doesn't just throw out `Expression` or whatnot. It probably has to do with the spot that it's in (declaration).Yes, parsing arbitrary expressions after an ` ` would result in this: ```D void f(int x) att in (x > 0) { } ``` Being parsed as: ```D void f(int x) (att in (x > 0)) { } ``` And things like ` 3 + 3` don't look like they would be parsed as ` (3 + 3)`, it looks like `( 3) + 3`. So the syntax as ` (expression)` to make it clear where the expression ends. Then there's ` identifier` and ` identifier(args)` as shorthand for common cases that do look clear. I recently added ` TemplateSingleArgument` so you can do ` "abc"` or ` 3` as well. Perhaps the syntax can be expanded to allow ` a.b.c(d)` as well, as well as ` a.b.c!d`, though there's a risk of the rules getting convoluted.
Jul 28 2023
Whenever there might be symbol clash, or when i want to make sure i can identify where something from from i do: ```d import me = my.awesome.module; void main() { me.hi(); } ```
Jul 28 2023