SHOW:
|
|
- or go back to the newest paste.
| 1 | struct S(T) {
| |
| 2 | T[] data; | |
| 3 | ||
| 4 | int opApply(int delegate (T*) dg) {
| |
| 5 | for(size_t i = 0; i < data.length; i++) {
| |
| 6 | int result = dg(&data[i]); | |
| 7 | if(result != 0) | |
| 8 | return result; | |
| 9 | } | |
| 10 | ||
| 11 | return 0; | |
| 12 | } | |
| 13 | ||
| 14 | int opApply(int delegate (const T*) dg) const {
| |
| 15 | for(size_t i = 0; i < data.length; i++) {
| |
| 16 | int result = dg(&data[i]); | |
| 17 | if(result != 0) | |
| 18 | return result; | |
| 19 | } | |
| 20 | ||
| 21 | return 0; | |
| 22 | } | |
| 23 | ||
| 24 | int opApply(int delegate (size_t, T*) dg) {
| |
| 25 | for(size_t i = 0; i < data.length; i++) {
| |
| 26 | int result = dg(i, &data[i]); | |
| 27 | if(result != 0) | |
| 28 | return result; | |
| 29 | } | |
| 30 | ||
| 31 | return 0; | |
| 32 | } | |
| 33 | ||
| 34 | int opApply(int delegate (size_t, const T*) dg) const {
| |
| 35 | for(size_t i = 0; i < data.length; i++) {
| |
| 36 | int result = dg(i, &data[i]); | |
| 37 | if(result != 0) | |
| 38 | return result; | |
| 39 | } | |
| 40 | ||
| 41 | return 0; | |
| 42 | } | |
| 43 | } | |
| 44 | ||
| 45 | void main() {
| |
| 46 | auto s1 = S!int([0, 1, 2, 3]); | |
| 47 | auto s2 = const S!int([0, 1, 2, 3]); | |
| 48 | ||
| 49 | import std.stdio : writeln, writefln; | |
| 50 | writeln("S!int, 1 arg:");
| |
| 51 | foreach(s; s1) | |
| 52 | (*s)++; | |
| 53 | ||
| 54 | writeln("S!int, 2 args:");
| |
| 55 | foreach(i, s; s1) | |
| 56 | writefln("%s: %s", i, *s);
| |
| 57 | ||
| 58 | writeln("const S!int, 1 arg:");
| |
| 59 | foreach(s; s2) | |
| 60 | writeln(*s); | |
| 61 | ||
| 62 | writeln("const S!int, 2 args:");
| |
| 63 | foreach(i, s; s2) | |
| 64 | writefln("%s: %s", i, *s);
| |
| 65 | } |