99struct Top end
1010
1111# get the type from a named tuple, given a name
12- get_type (NT, k) = hasfield (NT, k) ? fieldtype (NT, k) : Nothing
12+ get_type (NT, k) = hasfield (NT, k) ? fieldtype (NT, k) : Missing
1313
1414# unify two types to a single type
1515function promoteunion (T, S)
@@ -36,7 +36,7 @@ function unify(
3636 for (k, v) in zip (B, fieldtypes (b))
3737 if ! (k in ks)
3838 push! (ks, k)
39- push! (ts, unify (v, Nothing ))
39+ push! (ts, unify (v, Missing ))
4040 end
4141 end
4242
@@ -161,13 +161,27 @@ function generate_expr!(
161161 mutable:: Bool = true ,
162162) where {N,T<: Tuple }
163163 sub_exprs = []
164+ missing_fields = []
164165 for (n, t) in zip (N, fieldtypes (nt))
165166 push! (sub_exprs, generate_field_expr! (exprs, t, n; mutable = mutable))
167+ if Missing <: t
168+ push! (missing_fields, n)
169+ end
166170 end
167171
168172 struct_name = pascalcase (root_name)
173+ missing_kw_args = (length (missing_fields) > 0 ? " ; " : " " ) * join (map (n -> " n=missing" , missing_fields), " , " )
169174 if mutable
170- push! (sub_exprs, Meta. parse (" $struct_name () = new()" ))
175+ if length (missing_fields) == 0
176+ push! (sub_exprs, Meta. parse (" $struct_name () = new()" ))
177+ else
178+ missing_assigns = join (map (n -> " x.$n = missing" , missing_fields), " \n " )
179+ push! (sub_exprs, Meta. parse (""" function $struct_name ()
180+ x = new()
181+ $missing_assigns
182+ return x
183+ end""" ))
184+ end
171185 end
172186
173187 push! (exprs, Expr (:struct , mutable, struct_name, Expr (:block , sub_exprs... )))
0 commit comments