[Issue 17953] inout-like mechanism to infer function attributes from callback attributes
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Jun 24 11:32:02 UTC 2024
https://issues.dlang.org/show_bug.cgi?id=17953
Bolpat <qs.il.paperinik at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |qs.il.paperinik at gmail.com
--- Comment #3 from Bolpat <qs.il.paperinik at gmail.com> ---
This can be done using templates:
```d
int f(DG)(scope DG callback)
{
return callback("Hello, World");
}
void main() @safe
{
import std.stdio;
f((string str) { writeln(str); return 0; });
}
```
For opApply, for some reason, this works:
```d
struct S
{
private int opApplyImpl(DG)(scope DG dg)
{
static if (is(DG : int delegate(string)))
return dg("Hello, World!");
else
return dg(0, "Hello, World!");
}
alias opApply = opApplyImpl!(int delegate(string));
alias opApply = opApplyImpl!(int delegate(size_t, string));
}
void main() @safe
{
import std.stdio;
foreach (s; S())
{
static assert(is(typeof(s) == string));
writeln(s);
}
foreach (i, s; S())
{
static assert(is(typeof(i) == size_t));
static assert(is(typeof(s) == string));
writeln(i, " ", s);
}
}
```
--
More information about the Digitalmars-d-bugs
mailing list