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

all: implement multiple generics (#8231)

This commit is contained in:
Daniel Däschle
2021-01-22 13:49:56 +01:00
committed by GitHub
parent b10b76bb0d
commit 500ebf77e4
23 changed files with 499 additions and 245 deletions

View File

@@ -324,7 +324,7 @@ pub:
pos token.Position // function declaration position
body_pos token.Position // function bodys position
file string
is_generic bool
generic_params []GenericParam
is_direct_arr bool // direct array access
attrs []table.Attr
pub mut:
@@ -336,6 +336,11 @@ pub mut:
scope &Scope
}
pub struct GenericParam {
pub:
name string
}
// break, continue
pub struct BranchStmt {
pub:
@@ -362,7 +367,7 @@ pub mut:
receiver_type table.Type // User
return_type table.Type
should_be_skipped bool
generic_type table.Type // TODO array, to support multiple types
generic_types []table.Type
generic_list_pos token.Position
free_receiver bool // true if the receiver expression needs to be freed
scope &Scope

View File

@@ -59,8 +59,16 @@ pub fn (node &FnDecl) stringify(t &table.Table, cur_mod string, m2a map[string]s
if name in ['+', '-', '*', '/', '%', '<', '>', '==', '!=', '>=', '<='] {
f.write(' ')
}
if node.is_generic {
f.write('<T>')
if node.generic_params.len > 0 {
f.write('<')
for i, param in node.generic_params {
is_last := i == node.generic_params.len - 1
f.write(param.name)
if !is_last {
f.write(', ')
}
}
f.write('>')
}
f.write('(')
for i, arg in node.params {