Comment #0 by andrej.mitrovich — 2023-06-26T11:24:36Z
-----
// attribute
struct Struct { string name; }
// try to retrieve attributes here
void test(T)(T t) {
pragma(msg, __traits(getAttributes, T));
}
@Struct("foo") void foo() { }
void main() {
test(&foo); // FAIL: tuple()
}
-----
It seems that when function pointers are involved the UDAs get lost during IFTI.
For regular structure pointers it is possible to capture UDAs even during IFTI. Here's an example:
-----
import std.meta;
// attribute
struct Struct { string name; }
// try to retrieve attributes here
void test(T)(T t) {
static if (is(T X : E*, E)) {
pragma(msg, __traits(getAttributes, E)); // ok
}
}
@Struct("foo") struct Mine { }
void main() {
Mine mine;
test(&mine); // ok: tuple(Struct("foo"))
}
-----
However the same is not possible with function pointers:
-----
import std.meta;
// attribute
struct Struct { string name; }
// try to retrieve attributes here
void test(X)(X t) {
static if (is(X: E*, E)) {
pragma(msg, __traits(getAttributes, E));
}
}
@Struct("foo") void foo() { }
void main() {
test(&foo); // FAIL: tuple()
}
-----
The only alternative is to pass these functions as `alias` parameters. But that can only work with functions in some cases, and won't work with delegates.
Comment #1 by dkorpel — 2023-06-26T13:33:09Z
It works with structs because structs have a unique type from which you can retrieve UDAs, but functions don't. Including UDAs in function types affects a lot for the worse:
- type mangling (gets longer, more complex)
- rules regarding covariance/implicit conversion, overloading, overriding
- template instance re-use
I think you should use `alias` parameters instead. You say it doesn't work in some cases, but if in those cases it could actually be made to work, perhaps file an enhancement request for that instead?
Comment #2 by robert.schadek — 2024-12-13T19:29:52Z