On Sunday, 25 August 2024 at 05:07:43 UTC, Manu wrote:
> Like, from my example above, what even IS s.tupleof
? It's some kind of
list of what kind of thing? Direct symbol references to members of a live
instance?
It's a symbol sequence of references to fields.
import std;
struct S
{
int i;
char c;
}
void main()
{
S s = {2, 'c'};
ref si = s.i;
ref sc = s.c;
alias tupleof = AliasSeq!(si, sc); // same as s.tupleof
tupleof[0]++;
s.writeln();
assert(s.i == 3);
// this actually works
alias a = tupleof[0];
a++;
assert(s.i == 4);
}
So alias e = s.tupleof[0];
could be made to work.
See also: https://dlang.org/spec/template.html#lvalue-sequences.
BTW I'd like to make sequences a separate spec page from templates really.
> If s.tupleof can populate a list with that kind of thing, why doesn't this
work:
struct S
{
int x, y;
}
struct T
{
S s;
int i;
alias a = i; // works
alias b = s.x; // Doesn't work? Why?
}
a
is a symbol alias. b
would be an expression if it did what you want. Instead it's the same as S.x
, which needs an instance of S to use it at runtime.
However, you could now use ref b = s.x;
instead inside a method of T.
...
> alias b = s.tupleof[0]; // this emits a surprising error message:
error : alias b
cannot alias an expression AliasSeq!(s.x, s.y)[0]
So, that 'list' I mention; does this error message imply that this list given by tupleof
is an AliasSeq? What exactly IS an AliasSeq? What is the actual set of things that it can hold?
Symbols (declarations) and compile-time values. S.x is a declaration, s.x is a runtime value.
tupleof
is a symbol sequence of implicit ref declarations.