www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to get the location (file, line) of UDA without parens?

reply Andrey Zherikov <andrey.zherikov gmail.com> writes:
Here is my code:
```d
struct L
{
     string file;
     size_t line;
}

auto W(string f = __FILE__,size_t l= __LINE__)()
{
     return L(f,l);
}

struct A
{

   {
     int a;
     int b;
   }

   {
     int c;
     int d;
   }
}

void main()
{
     pragma(msg, __traits(getAttributes, A.a)); // tuple(W(string 
f = __FILE__, ulong l = __LINE__)())
     pragma(msg, __traits(getAttributes, A.b)); // tuple(W(string 
f = __FILE__, ulong l = __LINE__)())
     pragma(msg, __traits(getAttributes, A.c)); // 
tuple(L("app.d", 21LU))
     pragma(msg, __traits(getAttributes, A.d)); // 
tuple(L("app.d", 21LU))
}
```

`W()` (2) works as expected but is it possible to achieve the 
same without parenthesis so `getAttributes` trait returns 
`tuple(L("app.d", 16LU))` for (1)?
Nov 13 2021
parent reply user1234 <user1234 12.de> writes:
On Sunday, 14 November 2021 at 05:12:58 UTC, Andrey Zherikov 
wrote:
 Here is my code:
 [...]
 `W()` (2) works as expected but is it possible to achieve the 
 same without parenthesis so `getAttributes` trait returns 
 `tuple(L("app.d", 16LU))` for (1)?
No, without parens this is really the function template, as a symbol, that becomes the UDA.
Nov 13 2021
parent Andrey Zherikov <andrey.zherikov gmail.com> writes:
On Sunday, 14 November 2021 at 07:06:25 UTC, user1234 wrote:
 On Sunday, 14 November 2021 at 05:12:58 UTC, Andrey Zherikov 
 wrote:
 Here is my code:
 [...]
 `W()` (2) works as expected but is it possible to achieve the 
 same without parenthesis so `getAttributes` trait returns 
 `tuple(L("app.d", 16LU))` for (1)?
No, without parens this is really the function template, as a symbol, that becomes the UDA.
Can this be achieved with any other type of `W`? For example I can get the same result with struct (also changing ` W()` to ` W!()` on line (2)): ```d struct W(string f = __FILE__, size_t l = __LINE__) { auto loc = L(f,l); } ```
Nov 14 2021