Bug 18969 – 'need this for <function>' when passing field or method as template parameter
Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2018-06-11T13:18:18Z
Last change time
2023-03-08T15:09:10Z
Assigned to
No Owner
Creator
Simen Kjaeraas
Comments
Comment #0 by simen.kjaras — 2018-06-11T13:18:18Z
Consider this code:
class C { int n; }
void fun(alias fn)() {}
unittest {
C c = new C();
fun!(c.n)();
}
Error message:
foo.d(7): Error: need this for fun of type pure nothrow @nogc @safe void()
What's actually needed here is for fun to get a context pointer, either to the unittest block or to the instance of C.
Comment #1 by simen.kjaras — 2018-06-20T07:19:51Z
Related:
import std.meta : Alias;
alias getMember(alias T, string name) = Alias!(__traits(getMember, T, name));
struct S {
int i = 0;
}
unittest {
S s;
getMember!(s, "i") = 2;
}
Comment #2 by razvan.nitu1305 — 2023-03-08T15:09:10Z
I cannot reproduce this. Closing as WORKSFORME. Please reopen if I am missing something.