www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - cannot use UDA with same name as one of field's name

reply "sigod" <sigod.mail gmail.com> writes:
Hi. I have few questions about this piece of code.

```
import vibe.data.serialization;

struct User
{
	 name("_id") int id; // Error: function expected before (), not 
name of type string
	string name;
}
```

Is it even proper compiler behavior? Is there any way to bypass 
it without using alias (e.g. `alias named = name;`)?
Jun 23 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/23/15 4:53 PM, sigod wrote:
 Hi. I have few questions about this piece of code.

 ```
 import vibe.data.serialization;

 struct User
 {
       name("_id") int id; // Error: function expected before (), not
 name of type string
      string name;
 }
 ```

 Is it even proper compiler behavior?
Yes, name lookup looks in local scope first, then outside. D does not have a requirement to order declarations that aren't inside a function body.
 Is there any way to bypass it
 without using alias (e.g. `alias named = name;`)?
You can use full.path.name You may be able to use .name, but I'm not sure that parses. -Steve
Jun 23 2015
parent reply "sigod" <sigod.mail gmail.com> writes:
On Tuesday, 23 June 2015 at 22:10:43 UTC, Steven Schveighoffer 
wrote:
 You can use  full.path.name
``` Error: unexpected ( in declarator Error: basic type expected, not "_id" Error: found '"_id"' when expecting ')' Error: no identifier for declarator .data.serialization.name(int) Error: semicolon expected following function declaration Error: declaration expected, not ')' ```
 You may be able to use  .name, but I'm not sure that parses.
``` Error: identifier or (ArgumentList) expected, not . Error: valid attributes are property, safe, trusted, system, disable Error: unexpected ( in declarator Error: basic type expected, not "_id" Error: found '"_id"' when expecting ')' Error: no identifier for declarator .name(int) Error: semicolon expected following function declaration Error: declaration expected, not ')' ```
Jun 23 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/23/15 7:00 PM, sigod wrote:
 On Tuesday, 23 June 2015 at 22:10:43 UTC, Steven Schveighoffer wrote:
 You can use  full.path.name
``` Error: unexpected ( in declarator Error: basic type expected, not "_id" Error: found '"_id"' when expecting ')' Error: no identifier for declarator .data.serialization.name(int) Error: semicolon expected following function declaration Error: declaration expected, not ')' ```
I'm not completely sure on the syntax, try adding some parens. -Steve
Jun 23 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Tuesday, 23 June 2015 at 23:14:13 UTC, Steven Schveighoffer 
wrote:
 I'm not completely sure on the syntax, try adding some parens.
Yeah, I'm pretty sure it needs to be (full.name.here) void foo()
Jun 23 2015
parent "sigod" <sigod.mail gmail.com> writes:
On Tuesday, 23 June 2015 at 23:52:52 UTC, Adam D. Ruppe wrote:
 On Tuesday, 23 June 2015 at 23:14:13 UTC, Steven Schveighoffer 
 wrote:
 I'm not completely sure on the syntax, try adding some parens.
Yeah, I'm pretty sure it needs to be (full.name.here) void foo()
Yep, something like this works. ``` (vibe.data.serialization.name("_id")) int id; ``` But in this case `alias` looks better.
Jun 23 2015