Skip to content

invalid compiler error for generic function #2802

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

Closed
emekoi opened this issue Jul 2, 2019 · 1 comment
Closed

invalid compiler error for generic function #2802

emekoi opened this issue Jul 2, 2019 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@emekoi
Copy link
Contributor

emekoi commented Jul 2, 2019

const std = @import("std");

pub fn Iterator(comptime T: type) type {
    return struct {
        const Self = @This();
        nextFn: fn next(self: *Self) ?T,
    };
}

pub fn StepBy(comptime T: type) type {
    return struct {
        parent: *Iterator(T),
        step: usize = 1,

        pub fn new(iter: *Iterator(T), n: usize) Iterator(T) {
            return StepBy(T){
                // .nextFn = undefined,
                .parent = iter,
                .step = n,
            };
        }
    };
}

test "iterator.StepBy" {
    _ = StepBy(usize).new(undefined, 9);
}

generates the error:

error: no member named 'parent' in struct 'Iterator(usize)'
                .parent = iter,

but

const std = @import("std");

pub fn Iterator(comptime T: type) type {
    return struct {
        const Self = @This();
        nextFn: fn next(self: *Self) ?T,
    };
}

pub fn StepBy(comptime T: type) type {
    return struct {
        parent: *Iterator(T),
        step: usize = 1,

        pub fn new(iter: *Iterator(T), n: usize) Iterator(T) {
            return StepBy(T){
                .nextFn = undefined,
                // .parent = iter,
                // .step = n,
            };
        }
    };
}

test "iterator.StepBy" {
    _ = StepBy(usize).new(undefined, 9);
}

generates

error: no member named 'nextFn' in struct 'StepBy(usize)'
                .nextFn = undefined,
@andrewrk andrewrk added this to the 0.5.0 milestone Jul 2, 2019
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Jul 2, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 11, 2019
@andrewrk andrewrk added the stage1 The process of building from source via WebAssembly and the C backend. label Feb 10, 2020
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 10, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 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
@nektro
Copy link
Contributor

nektro commented Oct 12, 2021

the latter example now results in error: expected type 'StepBy(usize)', found 'Iterator(usize)' which gets to the issue that the defined and actual return type do not match

Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 29, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 29, 2022
TUSF pushed a commit to TUSF/zig that referenced this issue May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

3 participants