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

Generic return type for closure returned from function doesn't work #13253

Closed
svelterust opened this issue Jan 23, 2022 · 3 comments · Fixed by #15529
Closed

Generic return type for closure returned from function doesn't work #13253

svelterust opened this issue Jan 23, 2022 · 3 comments · Fixed by #15529
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor.

Comments

@svelterust
Copy link

V version: 0.2.4
OS: Arch Linux

What did you do?

fn closure<T>(input T) fn () T {
    return fn [input] () T {
        return input
    }
}

fn main() {
    closure(123)
}

What did you expect to see?
No error.

What did you see instead?

./bug.v:4:12: error: generic function declaration must specify generic type names, e.g. foo<T>
    2 | 
    3 | fn closure<T>(input T) fn () T {
    4 |     return fn [input] () T {
      |            ~~~~~~~~~~~~~~~~~
    5 |         return input
    6 |     }

I tried adding generic type name, but that resulted in this:

./bug.v:4:14: error: unexpected token `<`, expecting `(`
    2 | 
    3 | fn closure<T>(input T) fn () T {
    4 |     return fn<T> [input] () T {
      |              ^
    5 |         return input
    6 |     }
@svelterust svelterust added the Bug This tag is applied to issues which reports bugs. label Jan 23, 2022
@spytheman spytheman added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label May 27, 2022
@spytheman spytheman self-assigned this May 27, 2022
@vladimirmyshkovski
Copy link
Contributor

vladimirmyshkovski commented Jun 26, 2022

I have the same error.

image
image

@spytheman
Copy link
Member

fn closure<T>(input T) fn () T {
    return fn [input] <T>() T {
        return input
    }
}

fn main() {
    f := closure(123)
    dump(f())
}

now produces: [cc.v:9] main.f(): 123

@Cergoo
Copy link

Cergoo commented Aug 8, 2024

V 0.4.7 3ca5bc3
OS Linux Mint 22

but

fn closure<T>(input T) fn () []T {
    return fn [input] <T>() []T {
        return [input, input]
    }
}

fn main() {
    f := closure(123)
    dump(f())
}
Can't run code. The server returned an error:
code.v:2:12: error: cannot use `fn () []T` as type `fn () []int` in return argument
    1 | fn closure(input T) fn () []T {
    2 |     return fn [input] () []T {
      |            ~~~~~~~~~~~~~~~~~~~~~~
    3 |         return [input, input]
    4 |     }
Exited with error status 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants