0 issues 1 contributor 4 branches 0 releases
Additions: 38 Deletions: 12 View patch
1 got, exp := fun.got, fun.exp
2 got_sym, exp_sym := g.table.sym(got), g.table.sym(exp)
3 mut got_cname, exp_cname := got_sym.cname, exp_sym.cname
4+ mut type_idx := g.type_sidx(got)
5 if got_sym.info is ast.FnType {
6 if got_sym.info.is_anon {
7+ got_name := 'fn ${g.table.fn_type_source_signature(got_sym.info.func)}'
8 got_cname = 'anon_fn_${g.table.fn_type_signature(got_sym.info.func)}'
9+ type_idx = g.table.type_idxs[got_name].str()
10 }
11 }
12 mut sb := strings.new_builder(128)
13 // if the variable is not used, the C compiler will optimize it away
14 sb.writeln('\t$embed_cname* ${embed_name}_ptr = memdup($accessor, sizeof($embed_cname));')
15 }
16- sb.write_string('\treturn ($exp_cname){ ._$got_cname = ptr, ._typ = ${g.type_sidx(got)}')
17+ sb.write_string('\treturn ($exp_cname){ ._$got_cname = ptr, ._typ = $type_idx')
18 for field in (exp_sym.info as ast.SumType).fields {
19 mut ptr := 'ptr'
20 mut type_cname := got_cname
21
1-Fn
2-Fn
3+Fn(fn () int)
4+Fn(fn (int) int)
5+123
6+123
7+321
8+321
9
1 type Fn = fn () int | fn (int) int
2
3+fn abc(n int) Fn {
4+ if n == 1 {
5+ return fn () int {
6+ return 123
7+ }
8+ } else {
9+ return fn (x int) int {
10+ return x
11+ }
12+ }
13+}
14+
15 fn main() {
16- f1 := Fn(fn (a int) int {
17- return a
18- })
19- println(typeof(f1).name)
20-
21- f2 := Fn(fn () int {
22- return 22
23- })
24- println(typeof(f2).name)
25+ println(abc(1))
26+ println(abc(2))
27+
28+ x1 := abc(1)
29+ y1 := x1 as fn () int
30+ println(y1())
31+ if x1 is fn () int {
32+ println(x1())
33+ }
34+
35+ x2 := abc(2)
36+ y2 := x2 as fn (int) int
37+ println(y2(321))
38+ if x2 is fn (int) int {
39+ println(x2(321))
40+ }
41 }
42