diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1b4fc09dde..771202193e 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1375,10 +1375,13 @@ fn (mut g Gen) stmt(node ast.Stmt) { ast.Import {} ast.InterfaceDecl { // definitions are sorted and added in write_types - for method in node.methods { - if method.return_type.has_flag(.optional) { - // Register an optional if it's not registered yet - g.register_optional(method.return_type) + ts := g.table.get_type_symbol(node.typ) + if !(ts.info as ast.Interface).is_generic { + for method in node.methods { + if method.return_type.has_flag(.optional) { + // Register an optional if it's not registered yet + g.register_optional(method.return_type) + } } } } diff --git a/vlib/v/tests/generic_interface_test.v b/vlib/v/tests/generic_interface_test.v index 662855b5b3..059b1af7c7 100644 --- a/vlib/v/tests/generic_interface_test.v +++ b/vlib/v/tests/generic_interface_test.v @@ -63,3 +63,36 @@ fn test_extract_basic() { cc := extract_basic(c) assert '$aa | $bb | $cc' == '123 | 456 | 789' } + +////// +interface Iterator { + next() ?T +} + +struct NumberIterator { + limit T +mut: + val T +} + +fn (mut i NumberIterator) next() ?T { + if i.val >= i.limit { + return none + } + i.val++ + return i.val +} + +fn test_iterator_implementation() { + mut i := Iterator(NumberIterator{ + limit: 10 + }) + for { + if val := i.next() { + println(val) + } else { + println('iterator is done') + break + } + } +}