Skip to content
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

anytype does not propogate through functions #6423

Open
iansimonson opened this issue Sep 26, 2020 · 1 comment
Open

anytype does not propogate through functions #6423

iansimonson opened this issue Sep 26, 2020 · 1 comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@iansimonson
Copy link
Contributor

Given a function which takes an anytype and a function with signature dependent on that type, it's currently an unexpected compiler error to bind a function which takes anytype

Minimal Example:

const AType = struct {};

fn should_match(ctx: anytype, l: AType, r: AType) bool {
    return true;
}

fn foo(
    comptime T: type,
    context: anytype,
    comptime f: fn (context: @TypeOf(context), l: T, r: T) bool,
) void {}
pub fn main() void {
    foo(AType, .{}, should_match);
}

results in:

./anytype.zig:13:21: error: expected type 'fn(struct:13:17, AType, AType) bool', found 'fn(anytype,anytype,anytype) anytype'
    foo(AType, .{}, should_match);
                    ^
./anytype.zig:13:21: note: only one of the functions is generic
    foo(AType, .{}, should_match);
                    ^
./anytype.zig:13:8: note: referenced here
    foo(AType, .{}, should_match);
       ^

A more practical example is the use of std.sort.sort which takes a context. If no context is required it should be possible to pass it an empty struct literal (.{}). However the comparator used would then need to be able to handle the various literals passed through.

e.g. this should work:

const AType = struct { index: usize, };

fn compare_by_index(ignore_context: anytype, lhs: AType, rhs: AType) bool {
    return lhs.index < rhs.index;
}

// impl
var list: []AType = //make a list
std.sort.sort(AType, list, .{}, compare_by_index);
@Vexu
Copy link
Member

Vexu commented Sep 26, 2020

Related #4828.

@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Sep 26, 2020
@Vexu Vexu added this to the 0.8.0 milestone Sep 26, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@Vexu Vexu added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. and removed bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants