Alias example should supposedly be illegal, but runs fine

Meta jared771 at gmail.com
Tue Dec 19 01:29:04 UTC 2017


On Monday, 18 December 2017 at 23:44:46 UTC, Michael wrote:
> Hello,
>
> I have been looking at the following example found right at the 
> end of the section here: 
> https://dlang.org/spec/declaration.html#alias
>
> struct S { static int i; }
> S s;
>
> alias a = s.i; // illegal, s.i is an expression
> alias b = S.i; // ok
> b = 4;         // sets S.i to 4
>
> and it runs fine to me, including if I add:
>
> a = 3;
>
> So, to me I don't see why either can't be valid, but either way 
> something needs to be fixed to reflect that this is no longer 
> illegal in DMD v2.077.1.

I think the reason that this works is because i is static, 
meaning that you don't need the `this` reference of S to access 
it and thus it can be aliased. Declaring a static class or struct 
variable is pretty much the same as declaring a global variable, 
just with a tighter scope. If you look at it that way, then this 
makes a lot more sense. If you declare a global variable i at 
module scope, of course you can create an alias for it.


More information about the Digitalmars-d-learn mailing list