diff --git a/vlib/v/fmt/tests/interface_variadic_keep.vv b/vlib/v/fmt/tests/interface_variadic_keep.vv new file mode 100644 index 0000000000..ed288d32f9 --- /dev/null +++ b/vlib/v/fmt/tests/interface_variadic_keep.vv @@ -0,0 +1,4 @@ +interface Element { + unnamed_method(...f64) + named_method(params ...f64) +} diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index b059375370..8d61f89192 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -476,7 +476,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { return ast.InterfaceDecl{} } // field_names << name - args2, _, _ := p.fn_args() // TODO merge table.Param and ast.Arg to avoid this + args2, _, is_variadic := p.fn_args() // TODO merge table.Param and ast.Arg to avoid this mut args := [table.Param{ name: 'x' typ: typ @@ -489,6 +489,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { params: args file: p.file_name return_type: table.void_type + is_variadic: is_variadic is_pub: true pos: method_start_pos.extend(p.prev_tok.position()) scope: p.scope @@ -502,7 +503,13 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { method.next_comments = mnext_comments methods << method // println('register method $name') - ts.register_method(name: name, params: args, return_type: method.return_type, is_pub: true) + ts.register_method( + name: name + params: args + return_type: method.return_type + is_variadic: is_variadic + is_pub: true + ) } p.top_level_statement_end() p.check(.rcbr) diff --git a/vlib/v/tests/interface_variadic_test.v b/vlib/v/tests/interface_variadic_test.v new file mode 100644 index 0000000000..e49b6ba5cf --- /dev/null +++ b/vlib/v/tests/interface_variadic_test.v @@ -0,0 +1,25 @@ +interface Element { + method(params ...f64) string +} + +struct Foo {} + +fn (f &Foo) method(params ...f64) string { + return params.str() +} + +fn test_variadic_array_decompose() { + mut a := []Element{} + a << Foo{} + + input := [0.0, 1.0] + assert a[0].method(...input) == '[0, 1]' + assert a[0].method(...[0.0, 1.0]) == '[0, 1]' +} + +fn test_variadic_multiple_args() { + mut a := []Element{} + a << Foo{} + + assert a[0].method(0.0, 1.0) == '[0, 1]' +}