-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Proposal: Add function pointer field to TypeInfo.Definition.Data.FnDef #1135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is a bit tricky because the pointer type of the field would not be correct ( |
@andrewrk const assert = @import("std").debug.assert;
const WrappedFn = struct {
const Self = this;
fn_type: type,
fn_ptr: *const u8,
fn init(f: var) Self {
return Self { .fn_type=@typeOf(f), .fn_ptr=@ptrCast(*const u8, f), };
}
fn get_fn(comptime self: *const Self) self.fn_type {
return @ptrCast(self.fn_type, self.fn_ptr);
}
};
fn foo(arg: u8) u8 {
return arg;
}
test "comptime fn to u8 pointer casts" {
comptime {
const wrapped_foo = WrappedFn.init(foo);
assert(wrapped_foo.get_fn()(42) == 42);
}
}
EDIT So something similar could be used in FnDef: pub const FnDef = struct {
const Self = this;
fn_type: type,
inline_type: Inline,
calling_convention: CallingConvention,
is_var_args: bool,
is_extern: bool,
is_export: bool,
lib_name: ?[]const u8,
return_type: type,
arg_names: [][] const u8,
ptr: *const u8, // <- New field, guaranteed to point to fn of type 'fn_type'
pub const Inline = enum {
Auto,
Always,
Never,
};
// TODO: https://github.com/ziglang/zig/issues/1124
pub fn fn_ptr(comptime self: Self) self.fn_type {
return @ptrCast(self.fn_type, self.ptr);
}
}; So that the user needs only use the type safe fn_ptr method. |
I think this is a reasonable approach. Let me have a discussion about |
In addition to this, TypeInfo.Definition.Data.Var could be some type erased value (to allow reify-ing non type definitions) |
We now have the ability to have fields of type |
Great to see the comptime capabilities increasing, I'll have to catch up on the changes so I can maybe take a stab at fixing some bugs :) |
Proposed structure:
With the current structure, if @reify/@makeType was implemented, there would be no way to generate methods for a type.
This change would allow us to use the anonymous structure function trick or (eventually) function literals to generate methods for the types created.
EDIT:
Actually, this is just one of the changes that would need to be made to allow for method generation, the other issue being specifying the first argument type of the method when the type is not generated yet.
The text was updated successfully, but these errors were encountered: