`alias x = v.x;` not working in struct bodies?

Nick Treleaven nick at geany.org
Sat Jan 20 13:39:37 UTC 2024


On Saturday, 20 January 2024 at 09:49:06 UTC, Danilo wrote:
> On Saturday, 20 January 2024 at 09:00:22 UTC, Danilo wrote:
>> Documentation:
>> - https://dlang.org/spec/declaration.html#alias
>> - https://dlang.org/spec/declaration.html#alias-variable
>>
>> According to the link `alias-variable` I would expect my 
>> example to work.
>> Aliasing a variable (struct member), it's a symbol.
>
> We figured it out on Discord. `alias` works only with static 
> variables and members:
>
> ```d
> struct Vec2 {
>     static int x, y;
> }
>
> struct Vec3 {
>     static Vec2 v;
>     ...
> ```
>
> The documentation i mentioned above didn't explicitely say it's 
> for use with `static` variables/members only.

You can't alias fields of an aggregate type instance outside of 
the type definition. You can alias members of an aggregate type, 
such as static member variables. You can alias non-static members 
inside the aggregate definition.

```d
struct S
{
     int j;
     alias k = j;
}
```

I'll look at updating the spec as you're right, it's not clear.

BTW I have a PR which deprecates aliasing a member of a type 
instance:
https://github.com/dlang/dmd/pull/15863

I added a test case similar to your example.


More information about the Digitalmars-d mailing list