| 1 | const builtin = @import("builtin"); |
| 2 | const expect = @import("std").testing.expect; |
| 3 | |
| 4 | const Foo = struct { |
| 5 | a: u64 = 10, |
| 6 | |
| 7 | fn one(self: Foo) u64 { |
| 8 | return self.a + 1; |
| 9 | } |
| 10 | |
| 11 | const two = __two; |
| 12 | |
| 13 | fn __two(self: Foo) u64 { |
| 14 | return self.a + 2; |
| 15 | } |
| 16 | |
| 17 | const three = __three; |
| 18 | |
| 19 | const four = custom(Foo, 4); |
| 20 | }; |
| 21 | |
| 22 | fn __three(self: Foo) u64 { |
| 23 | return self.a + 3; |
| 24 | } |
| 25 | |
| 26 | fn custom(comptime T: type, comptime num: u64) fn (T) u64 { |
| 27 | return struct { |
| 28 | fn function(self: T) u64 { |
| 29 | return self.a + num; |
| 30 | } |
| 31 | }.function; |
| 32 | } |
| 33 | |
| 34 | test "fn delegation" { |
| 35 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 36 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 37 | |
| 38 | const foo = Foo{}; |
| 39 | try expect(foo.one() == 11); |
| 40 | try expect(foo.two() == 12); |
| 41 | try expect(foo.three() == 13); |
| 42 | try expect(foo.four() == 14); |
| 43 | } |