parser: implement generics method with auto multi generic types (#12312)
parent
e5c759eb91
commit
943a807d30
|
@ -315,10 +315,15 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||||
// <T>
|
// <T>
|
||||||
mut generic_names := p.parse_generic_names()
|
mut generic_names := p.parse_generic_names()
|
||||||
// generic names can be infer with receiver's 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)
|
sym := p.table.get_type_symbol(rec.typ)
|
||||||
if sym.info is ast.Struct {
|
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
|
// Args
|
||||||
|
|
|
@ -10,7 +10,27 @@ struct App<M> {
|
||||||
f 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 {
|
$if M is Something {
|
||||||
return 0
|
return 0
|
||||||
} $else {
|
} $else {
|
||||||
|
@ -26,5 +46,7 @@ fn test_generic_method_with_multi_types() {
|
||||||
i: 10
|
i: 10
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert app.next(1) == 0
|
assert app.next1(1) == 0
|
||||||
|
assert app.next2(1) == 0
|
||||||
|
assert app.next3(1) == 0
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue