If we can't access a struct/class member passed by alias (hopefully, this will be fixed some day), we at least should be able to get correct type information.
struct S
{
int x;
}
template Foo(T, alias u)
{
static assert(is(typeof(u) == T)); //fails
}
void main()
{
immutable s = S();
alias Foo!(typeof(s.x), s.x) foo;
}
The alias gets stripped of constness when passed to the template.
Comment #1 by razvan.nitu1305 — 2019-09-17T09:57:25Z
I think that passing a runtime variable to an alias parameter should be an error.
Comment #2 by maxsamukha — 2019-09-17T11:34:53Z
Would that break tons of reflective code like this?
// any template extracting compile-time info
template hasAttr(alias symbol, alias attr) {
import std.meta;
enum _f(alias s) = __traits(isSame, s, attr);
alias hasAttr = anySatisfy!(_f, __traits(getAttributes, symbol));
}
struct attr {
}
int main() {
@attr int x;
static assert(hasAttr!(x, attr)); // wouldn't work anymore
return 0;
}
Comment #3 by razvan.nitu1305 — 2019-09-17T11:38:08Z
(In reply to Max Samukha from comment #2)
> Would that break tons of reflective code like this?
>
> // any template extracting compile-time info
> template hasAttr(alias symbol, alias attr) {
> import std.meta;
> enum _f(alias s) = __traits(isSame, s, attr);
> alias hasAttr = anySatisfy!(_f, __traits(getAttributes, symbol));
> }
>
> struct attr {
> }
>
> int main() {
> @attr int x;
> static assert(hasAttr!(x, attr)); // wouldn't work anymore
> return 0;
> }
Yes. I tried working out a particular solution where only dotId expressions (such as the one on the original bug report) are excluded if the member is not a static one and it still breaks code in the dmd testsuite. It really is a rats nest.
Comment #4 by robert.schadek — 2024-12-13T17:49:29Z