diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 2b997d037f..c0dac59a0d 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1065,9 +1065,12 @@ fn (mut g Gen) expr_string_surround(prepend string, expr ast.Expr, append string // all unified in one place so that it doesnt break // if one location changes fn (mut g Gen) option_type_name(t ast.Type) (string, string) { - base := g.base_type(t) + mut base := g.base_type(t) mut styp := '' sym := g.table.sym(t) + if sym.info is ast.FnType { + base = 'anon_fn_${g.table.fn_type_signature(sym.info.func)}' + } if sym.language == .c && sym.kind == .struct_ { styp = '${c.option_name}_${base.replace(' ', '_')}' } else { @@ -1087,6 +1090,9 @@ fn (mut g Gen) result_type_name(t ast.Type) (string, string) { } mut styp := '' sym := g.table.sym(t) + if sym.info is ast.FnType { + base = 'anon_fn_${g.table.fn_type_signature(sym.info.func)}' + } if sym.language == .c && sym.kind == .struct_ { styp = '${c.result_name}_${base.replace(' ', '_')}' } else { diff --git a/vlib/v/tests/option_map_fn_type_value_test.v b/vlib/v/tests/option_map_fn_type_value_test.v new file mode 100644 index 0000000000..2ba561109a --- /dev/null +++ b/vlib/v/tests/option_map_fn_type_value_test.v @@ -0,0 +1,13 @@ +const mouse_action_to_function_map = { + 1: handle_mouse_down_signal +} + +fn handle_mouse_down_signal() string { + return 'hello' +} + +fn test_option_map_fn_type_value() { + t := mouse_action_to_function_map[1] or { return } + println(t()) + assert t() == 'hello' +}