diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index 770d3cb03e..2b0297ca9b 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -359,8 +359,7 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool { if field.has_default_expr { if sym.kind in [.sum_type, .interface_] { if field.typ.has_flag(.option) { - g.expr_opt_with_cast(field.default_expr, field.default_expr_typ.set_flag(.option), - field.typ) + g.expr_with_opt(field.default_expr, field.default_expr_typ, field.typ) } else { g.expr_with_cast(field.default_expr, field.default_expr_typ, field.typ) } diff --git a/vlib/v/tests/struct_field_option_type_with_default_value_init_test.v b/vlib/v/tests/struct_field_option_type_with_default_value_init_test.v new file mode 100644 index 0000000000..d154b540c2 --- /dev/null +++ b/vlib/v/tests/struct_field_option_type_with_default_value_init_test.v @@ -0,0 +1,13 @@ +type SomeType = SomeStruct | string + +struct SomeStruct {} + +struct AnotherStruct { + field ?SomeType = 'default_string' +} + +fn test_struct_field_option_type_with_default_value() { + s := AnotherStruct{} + println(s.field) + assert '${s.field}' == "Option(SomeType('default_string'))" +}