Since ceasing my use of D out of concern for the direction and health of the project, I've nonetheless been following these forums and have seen what I consider to be sincere efforts to improve things.
So I've started doing a bit of D work again and ran into this:
import std.stdio;
struct Foo {
int bar;
int baz;
}
void bletch(alias field)(Foo s, int x) {
s.field = x;
writeln("%d", s.field);
}
int main(string[] args)
{
Foo s;
bletch!bar(s);
bletch!baz(s);
return 0;
}
Compiling with dmd 2.106.0 on an up-to-date NixOS system results in
(dmd-2.106.0)dca@giovanni:/home/dca/Software/d_tests$ dmd test.d
test.d(16): Error: undefined identifier `bar`
test.d(17): Error: undefined identifier `baz`
So it appears that symbols passed to a function template alias parameter need to be in scope at the instantiation site as opposed to within the instantiated code.
This makes no sense to me. The symbols at the instantiation site are not being evaluated in that scope. They will be evaluated by the compiler when it gets its hands on the instantiated code. If a simple compile-time substitution were done in the above code, those symbols would both be defined in the instantiated code.
Reading the documentation, I'm guessing that template mixins can be used to address this problem, so it's probably the case (I haven't tried it) that I can achieve what I want that way.
But I'm failing to understand why the problem exists in the first place. Perhaps inherited from C++, which I do not know, having had some early exposure to it and concluded it was awful?
I'm probably missing something here, perhaps some important cases where the behavior I'm seeing does make sense. I'd be interested in the comments of those who know D (and perhaps C++) better than I do.