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

gen: fix autogenerated print methods for vargs

This commit is contained in:
Delyan Angelov 2020-05-24 14:59:52 +03:00
parent 465dc685cc
commit ddcb5f7da3
2 changed files with 52 additions and 3 deletions

View File

@ -3486,7 +3486,7 @@ fn (mut g Gen) gen_str_for_type_with_styp(typ table.Type, styp string) string {
if typ.flag_is(.variadic) {
varg_already_generated_key := 'varg_$already_generated_key'
if varg_already_generated_key !in g.str_types {
g.gen_str_for_varg(styp, str_fn_name)
g.gen_str_for_varg(styp, str_fn_name, sym_has_str_method)
g.str_types << varg_already_generated_key
}
return 'varg_$str_fn_name'
@ -3762,13 +3762,18 @@ fn (mut g Gen) gen_str_for_map(info table.Map, styp, str_fn_name string) {
g.auto_str_funcs.writeln('}')
}
fn (mut g Gen) gen_str_for_varg(styp, str_fn_name string) {
fn (mut g Gen) gen_str_for_varg(styp, str_fn_name string, has_str_method bool) {
g.definitions.writeln('string varg_${str_fn_name}(varg_$styp it); // auto')
g.auto_str_funcs.writeln('string varg_${str_fn_name}(varg_$styp it) {')
g.auto_str_funcs.writeln('\tstrings__Builder sb = strings__new_builder(it.len);')
g.auto_str_funcs.writeln('\tstrings__Builder_write(&sb, tos_lit("["));')
g.auto_str_funcs.writeln('\tfor(int i=0; i<it.len; i++) {')
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${str_fn_name}(it.args[i], 0));')
if has_str_method {
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${str_fn_name}(it.args[i]));')
}else{
// autogenerated str methods take the indent level as a second argument:
g.auto_str_funcs.writeln('\t\tstrings__Builder_write(&sb, ${str_fn_name}(it.args[i], 0));')
}
g.auto_str_funcs.writeln('\t\tif (i < it.len-1) {')
g.auto_str_funcs.writeln('\t\t\tstrings__Builder_write(&sb, tos_lit(", "));')
g.auto_str_funcs.writeln('\t\t}')

View File

@ -0,0 +1,44 @@
module main
fn test_autoprint_string_vargs() {
add_s('a')
assert true
add_s('a', 'b', 'c')
assert true
}
fn add_s(column string, other_columns ...string) {
println(column)
println(other_columns)
}
//
fn test_autoprint_int_vargs() {
add_i(1)
assert true
add_i(1, 2, 3)
assert true
}
fn add_i(column int, other_columns ...int) {
println(column)
println(other_columns)
}
//
struct Point {
x int
y int
}
fn test_autoprint_struct_vargs() {
add_point(Point{1, 2})
assert true
add_point(Point{1, 2}, Point{3, 4}, Point{5, 6})
assert true
}
fn add_point(column Point, other_columns ...Point) {
println(column)
println(other_columns)
}