1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix generics interface with multi generic structs (#10950)

This commit is contained in:
yuyi 2021-07-25 20:54:22 +08:00 committed by GitHub
parent a6245a56d8
commit ab20b8c53a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 0 deletions

View File

@ -180,6 +180,10 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
name = util.replace_op(name)
}
if node.is_method {
unwrapped_rec_sym := g.table.get_type_symbol(g.unwrap_generic(node.receiver.typ))
if unwrapped_rec_sym.kind == .placeholder {
return
}
name = g.cc_type(node.receiver.typ, false) + '_' + name
// name = g.table.get_type_symbol(node.receiver.typ).name + '_' + name
}

View File

@ -0,0 +1,58 @@
interface Iter<T> {
next() ?T
}
fn (it Iter<T>) skip<T>(n int) Iter<T> {
return SkipIter<T>{
n: n
}
}
struct ArrIter<T> {
data []T
mut:
index int
}
fn (mut it ArrIter<T>) next<T>() ?T {
if it.index >= it.data.len {
return none
}
defer {
it.index++
}
return it.data[it.index]
}
struct SkipIter<T> {
mut:
n int
iter Iter<T>
}
fn (mut it SkipIter<T>) next<T>() ?T {
for it.n > 0 {
it.iter.next() ?
it.n--
}
return it.iter.next()
}
fn test_generics_interface_with_multi_generic_structs() {
x := Iter<int>(ArrIter<int>{
data: [1, 2, 3]
})
println(x)
mut ret := x.next() or { 0 }
println(ret)
assert ret == 1
ret = x.next() or { 0 }
println(ret)
assert ret == 2
ret = x.next() or { 0 }
println(ret)
assert ret == 3
}