Skip to content

Commit

Permalink
Remove the failed closure to enable tests to run.
Browse files Browse the repository at this point in the history
  • Loading branch information
Makosai committed Aug 26, 2024
1 parent 35de170 commit 2c58702
Showing 1 changed file with 56 additions and 56 deletions.
112 changes: 56 additions & 56 deletions src/Utils/Extensions.zig
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
//! Extensions for various things you might find useful from other languages.

const std = @import("std");
const assert = std.debug.assert;
// const std = @import("std");
// const assert = std.debug.assert;

pub fn closure(bindings: anytype) ClosureInternal(@TypeOf(bindings)) {
return ClosureInternal(@TypeOf(bindings)){ .ctx = bindings };
}
// pub fn closure(bindings: anytype) ClosureInternal(@TypeOf(bindings)) {
// return ClosureInternal(@TypeOf(bindings)){ .ctx = bindings };
// }

fn ClosureInternal(comptime Spec: type) type {
comptime {
const spec_tinfo = @typeInfo(Spec);
assert(spec_tinfo == .Struct);
// fn ClosureInternal(comptime Spec: type) type {
// comptime {
// const spec_tinfo = @typeInfo(Spec);
// assert(spec_tinfo == .Struct);

for (spec_tinfo.Struct.fields) |field| {
assert(field.default_value == null);
}
// for (spec_tinfo.Struct.fields) |field| {
// assert(field.default_value == null);
// }

assert(spec_tinfo.Struct.decls.len == 1);
const call_decl = spec_tinfo.Struct.decls[0];
assert(call_decl.is_pub);
assert(std.mem.eql(u8, call_decl.name, "call"));
// assert(spec_tinfo.Struct.decls.len == 1);
// const call_decl = spec_tinfo.Struct.decls[0];
// // assert(call_decl.is_pub);
// assert(std.mem.eql(u8, call_decl.name, "call"));

const call = Spec.call;
const call_tinfo = @typeInfo(@TypeOf(call));
assert(call_tinfo == .Fn);
assert(!call_tinfo.Fn.is_generic);
assert(call_tinfo.Fn.params.len >= 1);
assert(call_tinfo.Fn.params[0].type.? == *const Spec);
// const call = Spec.call;
// const call_tinfo = @typeInfo(@TypeOf(call));
// assert(call_tinfo == .Fn);
// assert(!call_tinfo.Fn.is_generic);
// assert(call_tinfo.Fn.params.len >= 1);
// assert(call_tinfo.Fn.params[0].type.? == *const Spec);

var arg_types: [call_tinfo.Fn.params.len - 1]type = undefined;
for (call_tinfo.Fn.params[1..], 0..) |arg, i| {
arg_types[i] = arg.type.?;
}
// var arg_types: [call_tinfo.Fn.params.len - 1]type = undefined;
// for (call_tinfo.Fn.params[1..], 0..) |arg, i| {
// arg_types[i] = arg.type.?;
// }

const RetType = call_tinfo.Fn.return_type.?;
// const RetType = call_tinfo.Fn.return_type.?;

return Closure(Spec, arg_types[0..], RetType);
}
}
// return Closure(Spec, arg_types[0..], RetType);
// }
// }

pub fn Closure(comptime Ctx: type, comptime arg_types: []type, comptime RetType: type) type {
return struct {
ctx: Ctx,
// pub fn Closure(comptime Ctx: type, comptime arg_types: []type, comptime RetType: type) type {
// return struct {
// ctx: Ctx,

pub fn call(self: *const @This(), args: anytype) RetType {
comptime {
assert(args.len == arg_types.len);
for (args, 0..) |_, i| {
assert(@TypeOf(args[i]) == arg_types[i]);
}
}
return @call(.auto, Ctx.call, .{&self.ctx} ++ args);
}
};
}
// pub fn call(self: *const @This(), args: anytype) RetType {
// comptime {
// assert(args.len == arg_types.len);
// for (args, 0..) |_, i| {
// assert(@TypeOf(args[i]) == arg_types[i]);
// }
// }
// return @call(.auto, Ctx.call, .{&self.ctx} ++ args);
// }
// };
// }

test "closures" {
var x: i32 = 60;
// test "closures" {
// var x: i32 = 60;

const foo = closure(struct {
x: *i32,
pub fn call(self: *const @This(), y: i32) i32 {
self.x.* += y;
return 420;
}
}{ .x = &x });
// const foo = closure(struct {
// x: *i32,
// pub fn call(self: *const @This(), y: i32) i32 {
// self.x.* += y;
// return 420;
// }
// }{ .x = &x });

assert(foo.call(.{@as(i32, 9)}) == 420);
assert(x == 69);
}
// assert(foo.call(.{@as(i32, 9)}) == 420);
// assert(x == 69);
// }

0 comments on commit 2c58702

Please sign in to comment.