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 method unable to implement an interface with a different interface passed as argument #20670

Open
lazalong opened this issue Jan 27, 2024 · 0 comments · May be fixed by #20675
Open

Generic method unable to implement an interface with a different interface passed as argument #20670

lazalong opened this issue Jan 27, 2024 · 0 comments · May be fixed by #20675
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@lazalong
Copy link
Contributor

lazalong commented Jan 27, 2024

Describe the bug

Unable to create a generic method that requires to implement an interface with a different passed interface

Reproduction Steps

interface IModel {
    model string
}

interface ICar {
    name string
}

pub struct Car {
    name  string
    model string
}

fn main() {
    mut cars := []ICar{}
    cars << Car { model: 'Tesla' }
    cars << Car { model: 'Toyota' }

    // This works! So how to create a generic function out of this?
    mut cc := []IModel{}
    for mut c in cars {
        if mut c is IModel {
            cc << c
        }
    }

    a := get_all[IModel](mut cars)
    
    println("${cars} \n ${cc} \n ${a}")
}

pub fn get_all[T](mut cars []ICar) []T {
    mut cc := []T{}
    for mut c in cars {
        if mut c is T {  // error: cannot implement interface `ICar` with a different interface `IModel`
            cc << c
        }
    }
    return cc
}

Expected Behavior

See in the code sample below the get_all() function should return an array of IModel as does the code in the main()

Current Behavior

Throws an error: cannot implement interface ICar with a different interface IModel

Spytheman got it to generate an invalid C code :

Array_main__IModel main__get_all_T_main__IModel(Array_main__ICar* cars) {
    Array_main__IModel cc = __new_array_with_default(0, 0, sizeof(main__IModel), 0);
    for (int _t1 = 0; _t1 < cars->len; ++_t1) {
        main__ICar* c = ((main__ICar*)cars->data) + _t1;
        if ((c)->_typ == _main__ICar_main__IModel_index) {
            array_push((array*)&cc, _MOV((main__IModel[]){ *(I_main__ICar_as_I_main__IModel(*c)) }));
        }
    }
    return cc;
}

while in main (the non generic and working version), it is:

    Array_main__IModel cc = __new_array_with_default(0, 0, sizeof(main__IModel), 0);
    for (int _t3 = 0; _t3 < cars.len; ++_t3) {
        main__ICar* c = ((main__ICar*)cars.data) + _t3;
        if (I_main__ICar_is_I_main__IModel(*c)) {
            array_push((array*)&cc, _MOV((main__IModel[]){ (I_main__ICar_as_I_main__IModel(*c)) }));
        }
    }

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.4 35e91a7

Environment details (OS name and version, etc.)

V full version: V 0.4.4 ab4c1f4.35e91a7
OS: windows, Microsoft Windows 10 Pro v19045 64-bit
Processor: 8 cpus, 64bit, little endian,

getwd: C:\Users\Name
vexe: D:\Documents_Projects_VLang\v\v.exe
vexe mtime: 2024-01-13 07:59:37

vroot: OK, value: D:\Documents_Projects_VLang\v
VMODULES: OK, value: C:\Users\Name.vmodules
VTMP: OK, value: C:\Users\Name\AppData\Local\Temp\v_0

Git version: git version 2.9.0.windows.1
Git vroot status: 0.4.4-34-g35e91a7e (109 commit(s) behind V master)
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc status: thirdparty-windows-amd64 e90c2620

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.

@lazalong lazalong added the Bug This tag is applied to issues which reports bugs. label Jan 27, 2024
@spytheman spytheman added Unit: Checker Bugs/feature requests, that are related to the type checker. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Status: Confirmed This bug has been confirmed to be valid by a contributor. labels Jan 27, 2024
@felipensp felipensp self-assigned this Jan 27, 2024
@felipensp felipensp linked a pull request Jan 27, 2024 that will close this issue
@felipensp felipensp removed their assignment Feb 3, 2024
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. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants