vfmt: support `struct Repo <T, U> { }`

pull/6799/head
Delyan Angelov 2020-11-11 17:48:42 +02:00
parent 7d472d887e
commit ebfd259333
5 changed files with 38 additions and 2 deletions

View File

@ -7,7 +7,6 @@ import v.util
// os.v - // embeded comments, mib := [1/* CTL_KERN */, 14/* KERN_PROC */, 12/* KERN_PROC_PATHNAME */, -1] => comment the rest of the line
const (
known_failing_exceptions = [
'vlib/v/tests/generics_test.v', // struct Repo<T, U> { => struct Repo {
'vlib/crypto/aes/aes.v', // pub fn (c &AesCipher) encrypt(mut dst, mut src []byte) {
'vlib/crypto/aes/block_generic.v', // fn expand_key_generic(key []byte, mut enc, mut dec []u32) {
'vlib/crypto/aes/const.v', // multiple narrow columns of []string turned to 1 long single column, otherwise works

View File

@ -170,6 +170,7 @@ pub struct StructDecl {
pub:
pos token.Position
name string
gen_types []table.Type
is_pub bool
mut_pos int // mut:
pub_pos int // pub:

View File

@ -599,7 +599,14 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
}
f.write_language_prefix(node.language)
name := node.name.after('.')
f.writeln('$name {')
f.write(name)
if node.gen_types.len > 0 {
f.write(' <')
gtypes := node.gen_types.map(f.table.type_to_str(it)).join(', ')
f.write(gtypes)
f.write('>')
}
f.writeln(' {')
mut max := 0
mut max_type := 0
mut field_types := []string{cap: node.fields.len}

View File

@ -0,0 +1,28 @@
struct Foo <T> {
pub:
data T
}
fn (f Foo<int>) value() string {
return f.data.str()
}
type DB = string
struct Repo <T, U> {
db DB
pub mut:
model T
permission U
}
fn main() {
foo_int := Foo<int>{2}
assert foo_int.value() == '2'
println(foo_int)
//
x := Repo<int,f64>{'abc', 3, 1.5}
println(x.db)
println(x.model)
println(x.permission)
}

View File

@ -287,6 +287,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
is_union: is_union
attrs: attrs
end_comments: end_comments
gen_types: generic_types
}
}