digitalmars.D - A struct with a tuple as alias this, is kind of confusing
- Yuxuan Shui (12/12) Jul 27 2018 First, it surprised me that I can't index a struct like that. So:
- Yuxuan Shui (6/19) Jul 27 2018 Oh no, is it just defining arrays in the is() statement, though?
- ag0aep6g (6/30) Jul 27 2018 Looks like DMD decides that `C[0]` and `C[1]` can't be types in that
- Yuxuan Shui (2/18) Jul 27 2018 Let's just say this is confusing as hell.
First, it surprised me that I can't index a struct like that. So:
struct A(T...) {
alias S = T;
alias S this;
}
alias B = A!(int, double);
B[0] x; // Actually an array
Then, it surprised me again, that I actually can index it,
sometimes
static if (!is(B[0] == B[1]))
pragma(msg, "Works!");
Why is this language like this :(
Jul 27 2018
On Friday, 27 July 2018 at 10:17:21 UTC, Yuxuan Shui wrote:
First, it surprised me that I can't index a struct like that.
So:
struct A(T...) {
alias S = T;
alias S this;
}
alias B = A!(int, double);
B[0] x; // Actually an array
Then, it surprised me again, that I actually can index it,
sometimes
static if (!is(B[0] == B[1]))
pragma(msg, "Works!");
Why is this language like this :(
Oh no, is it just defining arrays in the is() statement, though?
But wait, this works:
alias C = A!(1,2,3);
static if (C[0] < C[1])
pragma(msg, "Ha!");
Jul 27 2018
On 07/27/2018 12:19 PM, Yuxuan Shui wrote:On Friday, 27 July 2018 at 10:17:21 UTC, Yuxuan Shui wrote:Yup.First, it surprised me that I can't index a struct like that. So: struct A(T...) { alias S = T; alias S this; } alias B = A!(int, double); B[0] x; // Actually an array Then, it surprised me again, that I actually can index it, sometimes static if (!is(B[0] == B[1])) pragma(msg, "Works!"); Why is this language like this :(Oh no, is it just defining arrays in the is() statement, though?But wait, this works: alias C = A!(1,2,3); static if (C[0] < C[1]) pragma(msg, "Ha!");Looks like DMD decides that `C[0]` and `C[1]` can't be types in that situation, so it tries the alias this. That's in line with how alias this is supposed to work: only kick in when the code wouldn't compile otherwise.
Jul 27 2018
On Friday, 27 July 2018 at 10:48:08 UTC, ag0aep6g wrote:On 07/27/2018 12:19 PM, Yuxuan Shui wrote:Let's just say this is confusing as hell.On Friday, 27 July 2018 at 10:17:21 UTC, Yuxuan Shui wrote:Yup.[...]Oh no, is it just defining arrays in the is() statement, though?But wait, this works: alias C = A!(1,2,3); static if (C[0] < C[1]) pragma(msg, "Ha!");Looks like DMD decides that `C[0]` and `C[1]` can't be types in that situation, so it tries the alias this. That's in line with how alias this is supposed to work: only kick in when the code wouldn't compile otherwise.
Jul 27 2018








Yuxuan Shui <yshuiv7 gmail.com>