Thread overview
Alias example should supposedly be illegal, but runs fine
Dec 18, 2017
Michael
Dec 19, 2017
codephantom
Dec 19, 2017
codephantom
Dec 19, 2017
Meta
Dec 19, 2017
Michael
Dec 19, 2017
Mike Franklin
Dec 19, 2017
codephantom
Dec 19, 2017
Mike Franklin
Dec 19, 2017
Michael
Dec 19, 2017
Mike Franklin
December 18, 2017
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.
December 19, 2017
On Monday, 18 December 2017 at 23:44:46 UTC, Michael wrote:
>
> alias a = s.i; // illegal, s.i is an expression


alias a = s.i; (this is an alias to a type, since s.i is an int)

Hence it is actually 'legal', as far as I understand.

i.e... "AliasDeclarations create a symbol that is an alias for another type, and can be used anywhere that other type may appear. "

What is 'illegal', is an alias to an expression.. for example:

alias strlen = string.sizeof;

December 19, 2017
On Monday, 18 December 2017 at 23:44:46 UTC, Michael wrote:
>
> alias a = s.i; // illegal, s.i is an expression

Actually, as I understand it, the example provided in 10. is legal (because it aliases a type), and the example provided in 3. is illegal (because it aliases an expression)

perhaps the examples in 10 and 3 should be swapped.

https://dlang.org/spec/declaration.html#alias

December 19, 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.
December 19, 2017
On Monday, 18 December 2017 at 23:44:46 UTC, Michael wrote:

> 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:

I think the example is wrong.  Consider this:

----
import std.stdio;

struct S
{
    static int i;
    int j;
}
S s;

void main()
{
    s.i = 1;
    s.j = 2;

    writeln(s.i);  // OK: Static symbols can be used through instances
    writeln(S.i);  // OK: Static symbols can be used through types
    writeln(s.j);  // OK: Instance symbols can be used through instances
    //writeln(S.j);  // Error: Instance symbols cannot be used through types.
}
----

https://run.dlang.io/is/eppwuf

Please file a bug report at http://issues.dlang.org/

Mike

December 19, 2017
On Tuesday, 19 December 2017 at 01:30:07 UTC, Mike Franklin wrote:
>
>  writeln(S.j);
>  // Error: Instance symbols cannot be used through types.

I don't understand why you would say that is a bug.

i.e.
// ------------
import std.stdio;

struct S
{
    int j;
}

void main()
{
    writeln(typeof(S.j).stringof);
   // prints: int
}
// -------------

"AliasDeclarations create a symbol that is an alias for another type, and can be used anywhere that other type may appear. ".

Since typeof S.j is an int, that seems consistent with this requirement, that alias is an alias for another type.

December 19, 2017
On Tuesday, 19 December 2017 at 02:04:34 UTC, codephantom wrote:

>>  writeln(S.j);
>>  // Error: Instance symbols cannot be used through types.
>
> I don't understand why you would say that is a bug.
>

I meant that the example is wrong, and a bug report should be filed to fix the example.

Mike
December 19, 2017
On Tuesday, 19 December 2017 at 01:29:04 UTC, Meta wrote:
> On Monday, 18 December 2017 at 23:44:46 UTC, Michael wrote:
>> [...]
>
> 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.

Yes I think you're right. I wasn't sure what was going on, just noticed that the example definitely isn't right. I'll file a bug support and try and fix it.
December 19, 2017
On Tuesday, 19 December 2017 at 02:12:29 UTC, Mike Franklin wrote:
> On Tuesday, 19 December 2017 at 02:04:34 UTC, codephantom wrote:
>
>>>  writeln(S.j);
>>>  // Error: Instance symbols cannot be used through types.
>>
>> I don't understand why you would say that is a bug.
>>
>
> I meant that the example is wrong, and a bug report should be filed to fix the example.
>
> Mike

Hmm.. but the example is explicitly dealing with when it is valid to create an alias for a non-static struct member. Should it still not be int? Even if you cannot change it via that alias?
December 19, 2017
On Tuesday, 19 December 2017 at 10:37:05 UTC, Michael wrote:
> On Tuesday, 19 December 2017 at 02:12:29 UTC, Mike Franklin wrote:
>> On Tuesday, 19 December 2017 at 02:04:34 UTC, codephantom wrote:
>>
>>>>  writeln(S.j);
>>>>  // Error: Instance symbols cannot be used through types.
>>>
>>> I don't understand why you would say that is a bug.
>>>
>>
>> I meant that the example is wrong, and a bug report should be filed to fix the example.
>>
>> Mike
>
> Hmm.. but the example is explicitly dealing with when it is valid to create an alias for a non-static struct member. Should it still not be int? Even if you cannot change it via that alias?

I don't quite understand what you mean.  `s.i` refers to a symbol in the compiler's symbol table.  Therefore, I don't see any reason it can't be aliased.  `alias a = b + c;` would be a better example to demonstrate that expressions cannot be aliased.

Mike