File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed
Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 1+ struct Options {
2+ a ? string
3+ b ? int
4+ c ? f64
5+ }
6+
7+ fn unwrap_not_none_field_types [T](t T) []string {
8+ mut arr := []string {}
9+ $for f in T.fields {
10+ v := t.$(f.name)
11+ $if f is $option {
12+ if v != none {
13+ arr << typeof (v).name
14+ }
15+ }
16+ }
17+ return arr
18+ }
19+
20+ fn test_main () {
21+ arr := unwrap_not_none_field_types (Options{
22+ a: 'x'
23+ b: 1
24+ c: 2.3
25+ })
26+ assert arr.join (' ' ) == 'string int f64'
27+ }
Original file line number Diff line number Diff line change @@ -71,8 +71,21 @@ pub fn (t &ResolverInfo) is_comptime_variant_var(node ast.Ident) bool {
7171
7272// typeof_type resolves type for typeof() expr where field.typ is resolved to real type instead of int type to make type(field.typ).name working
7373pub fn (mut t TypeResolver) typeof_type (node ast.Expr, default_type ast.Type) ast.Type {
74+ if node is ast.Ident {
75+ if t.info.inside_comptime_for && t.info.comptime_for_field_var != '' {
76+ if node.obj is ast.Var {
77+ obj_typ := node.obj.typ
78+ field_typ := t.info.comptime_for_field_type
79+ if (obj_typ.has_flag (.option) && field_typ.has_flag (.option))
80+ || (obj_typ.clear_flag (.option).idx () == field_typ.clear_flag (.option).idx ()
81+ && obj_typ.has_flag (.option)) {
82+ return field_typ.clear_flag (.option)
83+ }
84+ }
85+ }
86+ }
7487 if t.info.is_comptime (node) {
75- return t.get_type (node)
88+ return t.get_type (node). clear_flag (.option)
7689 } else if node is ast.SelectorExpr && node.expr_type != 0 {
7790 if node.expr is ast.Ident && node.is_field_typ {
7891 return t.get_type_from_comptime_var (node.expr)
You can’t perform that action at this time.
0 commit comments