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

fix fn type call

This commit is contained in:
BigBlack 2019-12-18 18:21:49 +08:00 committed by Alexander Medvednikov
parent 4b7aa4ec09
commit 2f218b878b
2 changed files with 28 additions and 4 deletions

View File

@ -1810,8 +1810,8 @@ fn (p mut Parser) var_expr(v Var) string {
p.next()
mut typ := v.typ
// Function pointer?
if typ.starts_with('fn ') && p.tok == .lpar {
T := p.table.find_type(typ)
if p.base_type(typ).starts_with('fn ') && p.tok == .lpar {
T := p.table.find_type(p.base_type(typ))
p.gen('(')
p.fn_call_args(mut T.func)
p.gen(')')
@ -1820,6 +1820,13 @@ fn (p mut Parser) var_expr(v Var) string {
// users[0].name
if p.tok == .lsbr {
typ = p.index_expr(typ, fn_ph)
if p.base_type(typ).starts_with('fn ') && p.tok == .lpar {
T := p.table.find_type(p.base_type(typ))
p.gen('(')
p.fn_call_args(mut T.func)
p.gen(')')
typ = T.func.typ
}
}
// a.b.c().d chain
// mut dc := 0
@ -1984,8 +1991,9 @@ pub:
}
', fname_tidx)
}
if p.base_type(field.typ).starts_with('fn ') && p.peek() == .lpar {
tmp_typ := p.table.find_type(field.typ)
base := p.base_type(field.typ)
if base.starts_with('fn ') && p.peek() == .lpar {
tmp_typ := p.table.find_type(base)
mut f := tmp_typ.func
p.gen('.$field.name')
p.gen('(')

View File

@ -143,5 +143,21 @@ fn test_assert_in_bool_fn() {
assert_in_bool_fn(2)
}
type MyFn fn (int) int
fn test(n int) int {
return n + 1000
}
struct MySt {
f MyFn
}
fn test_fn_type_call() {
mut arr := []MyFn
arr << MyFn(test)
assert arr[0](10) == 1010
st := MySt{f:test}
assert st.f(10) == 1010
}