Problem with templated alias as delegate parameter type
cc
cc at nevernet.com
Tue Jan 12 20:58:09 UTC 2021
Given the following program:
struct PingQuery {
string msg;
}
struct PingResponse {
string msg;
}
template send(T) {
void send(T query, void delegate(PingResponse) callback) {
writefln("Sending: %s", query);
if (callback) {
PingResponse resp;
resp.msg = query.msg;
callback(resp);
}
}
}
void main() {
send(PingQuery("helo"), (resp) {
writefln("Got response: %s", resp);
});
}
This works, but as you can see the PingResponse struct is
hardcoded in the send function. If I try to make it a part of
the template in a way such as this:
struct PingQuery {
alias RESPONSE = PingResponse;
string msg;
}
struct PingResponse {
string msg;
}
template send(T) {
static assert(is(T.RESPONSE == PingQuery.RESPONSE)); // this
succeeds at least
void send(T query, void delegate(T.RESPONSE) callback) {
writefln("Sending: %s", query);
if (callback) {
T.RESPONSE resp;
resp.msg = query.msg;
callback(resp);
}
}
}
void main() {
send(PingQuery("helo"), (resp) {
writefln("Got response: %s", resp);
});
}
I get:
delegatetest.d(48): Error: template `delegatetest.send` cannot
deduce function from argument types `!()(PingQuery, void)`,
candidates are:
delegatetest.d(35): `send(T)(T query, void
delegate(T.RESPONSE) callback)`
Same error if I use a nested struct (e.g. struct PingQuery {
struct RESPONSE {} }) instead of an alias. Currently using DMD32
D Compiler v2.095.0-dirty (win64).
More information about the Digitalmars-d-learn
mailing list