diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index 60c872a8c8d864..6d65d27b077f85 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -589,7 +589,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { } fn_name := c_fn_name(g.get_ternary_name(ident.name)) - if val_type.has_flag(.option) && val is ast.SelectorExpr { + if val_type.has_flag(.option) && val in [ast.CastExpr, ast.SelectorExpr] { ret_styp := g.typ(g.unwrap_generic(val_type)) g.write('${ret_styp} ${fn_name}') } else { diff --git a/vlib/v/tests/options/option_fntype_assign_test.v b/vlib/v/tests/options/option_fntype_assign_test.v new file mode 100644 index 00000000000000..d21bea4e2f54b9 --- /dev/null +++ b/vlib/v/tests/options/option_fntype_assign_test.v @@ -0,0 +1,16 @@ +type DataFn = fn (name string) string + +fn which_lang(name string) string { + return name +} + +fn find_func(name string) ?DataFn { + a := ?DataFn(which_lang) + return a +} + +fn test_main() { + if a := find_func('foo') { + assert a('bar') == 'bar' + } +}