parser: implement generics method with auto multi generic types (#12312)

pull/12320/head
yuyi 2021-10-27 20:41:13 +08:00 committed by GitHub
parent e5c759eb91
commit 943a807d30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 4 deletions

View File

@ -315,10 +315,15 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
// <T>
mut generic_names := p.parse_generic_names()
// generic names can be infer with receiver's generic names
if is_method && rec.typ.has_flag(.generic) && generic_names.len == 0 {
if is_method && rec.typ.has_flag(.generic) {
sym := p.table.get_type_symbol(rec.typ)
if sym.info is ast.Struct {
generic_names = sym.info.generic_types.map(p.table.get_type_symbol(it).name)
rec_generic_names := sym.info.generic_types.map(p.table.get_type_symbol(it).name)
for gname in rec_generic_names {
if gname !in generic_names {
generic_names << gname
}
}
}
}
// Args

View File

@ -10,7 +10,27 @@ struct App<M> {
f M
}
fn (mut self App<M>) next<M, T>(input T) f64 {
fn (mut self App<M>) next1<M, T>(input T) f64 {
$if M is Something {
return 0
} $else {
panic('${typeof(M.typ).name} is not supported')
return 1
}
return 1
}
fn (mut self App<M>) next2<T, M>(input T) f64 {
$if M is Something {
return 0
} $else {
panic('${typeof(M.typ).name} is not supported')
return 1
}
return 1
}
fn (mut self App<M>) next3<T>(input T) f64 {
$if M is Something {
return 0
} $else {
@ -26,5 +46,7 @@ fn test_generic_method_with_multi_types() {
i: 10
}
}
assert app.next(1) == 0
assert app.next1(1) == 0
assert app.next2(1) == 0
assert app.next3(1) == 0
}