Understanding alias template parameters
Salih Dincer
salihdb at hotmail.com
Thu Apr 21 23:02:00 UTC 2022
On Thursday, 21 April 2022 at 21:38:14 UTC, Ali Çehreli wrote:
> ```d
> auto myMap(alias f, R)(R r) {
> pragma(msg, typeof(f));
> return MapResult!(R, f)(r);
> }
> ```
It looks delicious when the convenience function works magic with
Voldemort:
```d
import std.range, std.stdio;
auto myMap(alias f, R)(R r) {
struct Map {
auto empty() {
return r.empty;
}
auto front() {
return f(r.front);
}
void popFront() {
r.popFront;
}
}
return Map();
}
void main() {
// with convenience function:
alias func = (int x) => 2 * x;
auto range = 1.iota(11);
range.myMap!func.writeln;
range.myMap!(x => 2 * x).writeln;
// with only struct:
struct MapResult(alias range, alias f) {
auto empty() { return range.empty; }
auto front() { return f(range.front); }
void popFront() { range.popFront; }
}
MapResult!(range, func) mp;
foreach(result; mp) result.writeln;
} /* OUTPUT:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
2
4
6
8
10
12
14
16
18
20
*/
```
SDB at 79
More information about the Digitalmars-d-learn
mailing list