mirror of
https://github.com/zigzap/zap.git
synced 2025-10-21 15:44:10 +00:00
WIP side-experiment WIP BoundFunction
This commit is contained in:
parent
7da0a6fe4e
commit
9d3434c21c
1 changed files with 36 additions and 0 deletions
36
src/BoundFunction.zig
Normal file
36
src/BoundFunction.zig
Normal 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);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue