digitalmars.D.learn - Alias example should supposedly be illegal, but runs fine
- Michael (14/14) Dec 18 2017 Hello,
- codephantom (8/9) Dec 18 2017 alias a = s.i; (this is an alias to a type, since s.i is an int)
- codephantom (6/7) Dec 18 2017 Actually, as I understand it, the example provided in 10. is
- Meta (8/22) Dec 18 2017 I think the reason that this works is because i is static,
- Michael (4/14) Dec 19 2017 Yes I think you're right. I wasn't sure what was going on, just
- Mike Franklin (26/35) Dec 18 2017 I think the example is wrong. Consider this:
- codephantom (19/21) Dec 18 2017 I don't understand why you would say that is a bug.
- Mike Franklin (4/7) Dec 18 2017 I meant that the example is wrong, and a bug report should be
- Michael (4/13) Dec 19 2017 Hmm.. but the example is explicitly dealing with when it is valid
- Mike Franklin (6/25) Dec 19 2017 I don't quite understand what you mean. `s.i` refers to a symbol
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.
Dec 18 2017
On Monday, 18 December 2017 at 23:44:46 UTC, Michael wrote:alias a = s.i; // illegal, s.i is an expressionalias 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;
Dec 18 2017
On Monday, 18 December 2017 at 23:44:46 UTC, Michael wrote:alias a = s.i; // illegal, s.i is an expressionActually, 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
Dec 18 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.
Dec 18 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: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.[...]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.
Dec 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
Dec 18 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.
Dec 18 2017
On Tuesday, 19 December 2017 at 02:04:34 UTC, codephantom wrote:I meant that the example is wrong, and a bug report should be filed to fix the example. Mikewriteln(S.j); // Error: Instance symbols cannot be used through types.I don't understand why you would say that is a bug.
Dec 18 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: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 meant that the example is wrong, and a bug report should be filed to fix the example. Mikewriteln(S.j); // Error: Instance symbols cannot be used through types.I don't understand why you would say that is a bug.
Dec 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: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. MikeOn Tuesday, 19 December 2017 at 02:04:34 UTC, codephantom wrote: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 meant that the example is wrong, and a bug report should be filed to fix the example. Mikewriteln(S.j); // Error: Instance symbols cannot be used through types.I don't understand why you would say that is a bug.
Dec 19 2017