Skip to content

Commit ca08ed1

Browse files
committed
test(reflect): add closure call cases
1 parent 3ad115b commit ca08ed1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

_demo/go/reflectfunc/reflectfunc.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,32 @@ func main() {
5454
if len(out) != 1 || out[0].Int() != 15 {
5555
panic(fmt.Sprintf("reflect closure call failed: got %v", out))
5656
}
57+
58+
// More complex closure calls: multi-args / multi-returns with free vars.
59+
base := 7
60+
scale := 3
61+
mix := func(a, b int, tag string) (int, string) {
62+
return base + a*scale + b, fmt.Sprintf("%s:%d", tag, base)
63+
}
64+
vmix := reflect.ValueOf(mix)
65+
mout := vmix.Call([]reflect.Value{
66+
reflect.ValueOf(2),
67+
reflect.ValueOf(5),
68+
reflect.ValueOf("k"),
69+
})
70+
if len(mout) != 2 || mout[0].Int() != 18 || mout[1].String() != "k:7" {
71+
panic(fmt.Sprintf("reflect closure multi call failed: got %v", mout))
72+
}
73+
74+
offset := 100
75+
swapAdd := func(a int, b int) (int, int) {
76+
return b + offset, a + offset
77+
}
78+
vswap := reflect.ValueOf(swapAdd)
79+
sout := vswap.Call([]reflect.Value{reflect.ValueOf(1), reflect.ValueOf(9)})
80+
if len(sout) != 2 || sout[0].Int() != 109 || sout[1].Int() != 101 {
81+
panic(fmt.Sprintf("reflect closure swap call failed: got %v", sout))
82+
}
5783
}
5884

5985
type T struct {

0 commit comments

Comments
 (0)