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

Type inference with generic structs and generic interfaces #19618

Open
tom65536 opened this issue Oct 21, 2023 · 1 comment
Open

Type inference with generic structs and generic interfaces #19618

tom65536 opened this issue Oct 21, 2023 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@tom65536
Copy link

tom65536 commented Oct 21, 2023

Describe the bug

Type inference does not bind generic structs as expected.
(Unless my expectation is wrong)

Reproduction Steps

module main

interface MyInterface[T] {
        is_good(a T) bool
}

struct MyStruct[T] {}

struct Opt[T] {
        x ?T
}

fn (_ MyStruct[T]) is_good(a Opt[T]) bool {
        if _ := a.x {
                return true
        }
        return false
}

fn check[T](a MyInterface[T], b T) {
        println('${a.is_good(b)}')
}

fn main() {
        a := MyStruct[int]{}
        b := Opt[int]{6}
        check(a, b)
}

Expected Behavior

In call to fn check, T should be bound as Opt[int] and MyStruct[int] should be accepted as implementation of MyInterface[Opt[int]].

Current Behavior

Looks like the compiler can only infer struct X[T] as implementation of interface Y[S] if S can be bound to T (as a type variable)

src/main.v:27:8: error: could not infer generic type `T` in interface `MyInterface`
   25 |     a := MyStruct[int]{}
   26 |     b := Opt[int]{6}
   27 |     check(a, b)
      |           ^
   28 | }
src/main.v:27:8: error: cannot use `MyStruct[int]` as `MyInterface[int]` in argument 1 to `check`
   25 |     a := MyStruct[int]{}
   26 |     b := Opt[int]{6}
   27 |     check(a, b)
      |           ^
   28 | }
src/main.v:27:11: error: cannot use `Opt[int]` as `int` in argument 2 to `check`
   25 |     a := MyStruct[int]{}
   26 |     b := Opt[int]{6}
   27 |     check(a, b)
      |              ^
   28 | }
src/main.v:21:23: error: cannot use `int` as `Opt[int]` in argument 1 to `MyInterface[int].is_good`
   19 |
   20 | fn check[T](a MyInterface[T], b T) {
   21 |     println('${a.is_good(b)}')
      |                          ^
   22 | }
   23 |

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.2 20a1af3

Environment details (OS name and version, etc.)

Android, Termux

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@tom65536 tom65536 added the Bug This tag is applied to issues which reports bugs. label Oct 21, 2023
@ArtemkaKun ArtemkaKun added Generics[T] Bugs/feature requests, that are related to the V generics. Unit: Checker Bugs/feature requests, that are related to the type checker. labels Oct 21, 2023
@yuyi98
Copy link
Member

yuyi98 commented Oct 30, 2023

fn (_ MyStruct[T]) is_good(a Opt[T]) bool {
        if _ := a.x {
                return true
        }
        return false
}

a Opt[T] ???
a T will only correspond to the interface method.

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. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
Development

No branches or pull requests

3 participants