1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 23:24:09 +00:00

WIP side-experiment WIP BoundFunction

This commit is contained in:
Rene Schallner 2025-03-21 20:08:43 +01:00 committed by renerocksai
parent 7da0a6fe4e
commit 9d3434c21c

36
src/BoundFunction.zig Normal file
View file

@ -0,0 +1,36 @@
const std = @import("std");
// attempt 1: explicitly typed
pub fn Create(Fn: type, Instance: type) type {
return struct {
instance: *Instance,
function: *const Fn,
const BoundFunction = @This();
pub fn init(function: *const Fn, instance: *Instance) BoundFunction {
return .{
.instance = instance,
.function = function,
};
}
pub fn call(self: *const BoundFunction, arg: anytype) void {
@call(.auto, self.function, .{ self.instance, arg });
}
};
}
test "BoundFunction" {
const X = struct {
field: usize = 0,
pub fn foo(self: *@This(), other: usize) void {
std.debug.print("field={d}, other={d}\n", .{ self.field, other });
}
};
var x: X = .{ .field = 27 };
var bound = Create(@TypeOf(X.foo), X).init(X.foo, &x);
bound.call(3);
}