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

cgen: fix generic struct to string (#10191)

This commit is contained in:
yuyi 2021-05-25 12:45:54 +08:00 committed by GitHub
parent 306c16f0fa
commit f3274700cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View File

@ -94,12 +94,12 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, styp string, str_fn_name stri
for i, field in info.fields {
mut ptr_amp := if field.typ.is_ptr() { '&' } else { '' }
base_fmt := g.type_to_fmt1(field.typ)
base_fmt := g.type_to_fmt1(g.unwrap_generic(field.typ))
// manage prefix and quote symbol for the filed
mut quote_str := ''
mut prefix := ''
sym := g.table.get_type_symbol(field.typ)
sym := g.table.get_type_symbol(g.unwrap_generic(field.typ))
if sym.kind == .string {
quote_str = "'"
} else if field.typ in ast.charptr_types {

View File

@ -0,0 +1,33 @@
struct Info<T> {
data T
}
fn get_info<T>(res Info<T>) string {
return '$res'
}
fn test_generic_struct_to_string() {
mut ret := get_info(Info<bool>{true})
println(ret)
assert ret.contains('data: true')
ret = get_info(Info<int>{123})
println(ret)
assert ret.contains('data: 123')
ret = get_info(Info<f32>{f32(2.2)})
println(ret)
assert ret.contains('data: 2.2')
ret = get_info(Info<f64>{2.2})
println(ret)
assert ret.contains('data: 2.2')
ret = get_info(Info<string>{'aaa'})
println(ret)
assert ret.contains("data: 'aaa'")
ret = get_info(Info<u64>{u64(234)})
println(ret)
assert ret.contains('data: 234')
}