Skip to content

Commit

Permalink
updated count example
Browse files Browse the repository at this point in the history
  • Loading branch information
permutationlock committed Nov 21, 2023
1 parent 5b922d0 commit 91b95b2
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions examples/count.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,30 @@ test "explicit implementation" {
try testing.expectEqual(@as(usize, 10), count);
}

test "infer implementation" {
const MyCounter = struct {
count: usize,
const MyCounter = struct {
count: usize,

pub fn increment(self: *@This()) void {
self.count += 1;
}
pub fn increment(self: *@This()) void {
self.count += 1;
}

pub fn read(self: *const @This()) usize {
return self.count;
}
};
pub fn read(self: *const @This()) usize {
return self.count;
}
};

test "infer implementation" {
var counter: MyCounter = .{ .count = 0 };
countToTen(&counter, .{});
try testing.expectEqual(@as(usize, 10), counter.count);
}

test "override implementation" {
const MyCounter = struct {
count: usize,

pub fn increment(self: *@This()) void {
self.count += 1;
}

pub fn read(self: *const @This()) usize {
return self.count;
}
};
fn otherInc(self: *MyCounter) void {
self.count = 1 + self.count * 2;
}

const S = struct {
pub fn incThree(self: *MyCounter) void {
self.count = 1 + self.count * 2;
}
};
test "override implementation" {
var counter: MyCounter = .{ .count = 0 };
countToTen(&counter, .{ .increment = S.incThree });
countToTen(&counter, .{ .increment = otherInc });
try testing.expectEqual(@as(usize, 15), counter.count);
}

0 comments on commit 91b95b2

Please sign in to comment.