@@ -142,34 +142,16 @@ end
142
142
143
143
pop_structure_dict! (dict, key) = length (dict[key]) == 0 && pop! (dict, key)
144
144
145
- create_kwarg_type (meta, where_types, type) = haskey (meta, VariableUnit) ? create_kwarg_type_ (meta[VariableUnit], where_types, type) : Expr (:curly , Union, Nothing, type)
146
- function create_kwarg_type_ (unitmacro, where_types, type)
147
- fn = gensym ()
148
- quote
149
- # Expr(:curly, Union, :Nothing, Expr(:curly, Unitful.Quantity, Expr(:(<:), type), D, units))
150
- function $fn ()
151
- let u = eval ($ unitmacro)
152
- if typeof (u) <: Unitful.FreeUnits
153
- Union{Nothing, Unitful. Quantity{<: $type , dimension (u), u}}
154
- elseif typeof (u) <: DynamicQuantities.Quantity
155
- Union{Nothing, DynamicQuantities. Quantity{<: $type , $ units}}
156
- else
157
- throw (" Unsupported units library" )
158
- end
159
- end
160
- end
161
- $ fn ()
162
- end
163
- end
164
- # function create_kwarg_type_(::DynamicQuantities.Quantity{T, D}, where_types, type) where {T, D}
165
- # Expr(:curly, Union, :Nothing, Expr(:curly, DynamicQuantities.Quantity, Expr(:(<:), type), D))
166
- # end
167
-
168
145
function update_kwargs_and_metadata! (dict, kwargs, a, def, indices, type, var,
169
146
varclass, where_types, meta)
170
147
if indices isa Nothing
171
- kwtype = create_kwarg_type (meta, where_types, type)
172
- push! (kwargs, Expr (:kw , Expr (:(:: ), a, kwtype), nothing ))
148
+ if ! isnothing (meta) && haskey (meta, VariableUnit)
149
+ uvar = gensym ()
150
+ push! (where_types, uvar)
151
+ push! (kwargs, Expr (:kw , :($ a:: Union{Nothing, $uvar} ), nothing ))
152
+ else
153
+ push! (kwargs, Expr (:kw , :($ a:: Union{Nothing, $type} ), nothing ))
154
+ end
173
155
dict[:kwargs ][getname (var)] = Dict (:value => def, :type => type)
174
156
else
175
157
vartype = gensym (:T )
190
172
191
173
function parse_variable_def! (dict, mod, arg, varclass, kwargs, where_types;
192
174
def = nothing , indices:: Union{Vector{UnitRange{Int}}, Nothing} = nothing ,
193
- meta = nothing , type:: Type = Real)
175
+ type:: Type = Real, meta = Dict {DataType, Expr} () )
194
176
metatypes = [(:connection_type , VariableConnectType),
195
177
(:description , VariableDescription),
196
178
(:unit , VariableUnit),
@@ -215,12 +197,12 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
215
197
end
216
198
Expr (:(:: ), a, type) => begin
217
199
type = getfield (mod, type)
218
- parse_variable_def! (dict, mod, a, varclass, kwargs, where_types; def, type)
200
+ parse_variable_def! (dict, mod, a, varclass, kwargs, where_types; def, type, meta )
219
201
end
220
202
Expr (:(:: ), Expr (:call , a, b), type) => begin
221
203
type = getfield (mod, type)
222
204
def = _type_check! (def, a, type, varclass)
223
- parse_variable_def! (dict, mod, a, varclass, kwargs, where_types; def, type)
205
+ parse_variable_def! (dict, mod, a, varclass, kwargs, where_types; def, type, meta )
224
206
end
225
207
Expr (:call , a, b) => begin
226
208
var = generate_var! (dict, a, b, varclass, mod; indices, type)
@@ -232,7 +214,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
232
214
Base. remove_linenums! (b)
233
215
def, meta = parse_default (mod, b)
234
216
var, def, _ = parse_variable_def! (
235
- dict, mod, a, varclass, kwargs, where_types; def, meta, type )
217
+ dict, mod, a, varclass, kwargs, where_types; def, type, meta )
236
218
if dict[varclass] isa Vector
237
219
dict[varclass][1 ][getname (var)][:default ] = def
238
220
else
@@ -257,7 +239,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
257
239
Expr (:tuple , a, b) => begin
258
240
meta = parse_metadata (mod, b)
259
241
var, def, _ = parse_variable_def! (
260
- dict, mod, a, varclass, kwargs, where_types; meta, type)
242
+ dict, mod, a, varclass, kwargs, where_types, meta; type, meta )
261
243
if meta != = nothing
262
244
for (type, key) in metatypes
263
245
if (mt = get (meta, key, nothing )) != = nothing
@@ -277,7 +259,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
277
259
Expr (:ref , a, b... ) => begin
278
260
indices = map (i -> UnitRange (i. args[2 ], i. args[end ]), b)
279
261
parse_variable_def! (dict, mod, a, varclass, kwargs, where_types;
280
- def, indices, type)
262
+ def, indices, type, meta )
281
263
end
282
264
_ => error (" $arg cannot be parsed" )
283
265
end
@@ -635,6 +617,14 @@ function parse_variable_arg!(exprs, vs, dict, mod, arg, varclass, kwargs, where_
635
617
push! (exprs, ex)
636
618
end
637
619
620
+ function convert_units (varunits:: DynamicQuantities.Quantity , value)
621
+ DynamicQuantities. ustrip (DynamicQuantities. uconvert (DynamicQuantities. SymbolicUnits. as_quantity (varunits), value))
622
+ end
623
+
624
+ function convert_units (varunits:: Unitful.FreeUnits , value)
625
+ Unitful. ustrip (varunits, value)
626
+ end
627
+
638
628
function parse_variable_arg (dict, mod, arg, varclass, kwargs, where_types)
639
629
vv, def, metadata_with_exprs = parse_variable_def! (
640
630
dict, mod, arg, varclass, kwargs, where_types)
@@ -646,7 +636,15 @@ function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types)
646
636
$ name = if $ name === nothing
647
637
$ setdefault ($ vv, $ def)
648
638
else
649
- $ setdefault ($ vv, $ ustrip ($ unit, $ name))
639
+ try
640
+ $ setdefault ($ vv, $ convert_units ($ unit, $ name))
641
+ catch e
642
+ if isa (e, DynamicQuantities. DimensionError) || isa (e, Unitful. DimensionError)
643
+ error (" Unable to convert units for \' " * string (:($$ vv))* " \' " )
644
+ else
645
+ rethrow (e)
646
+ end
647
+ end
650
648
end
651
649
end
652
650
else
0 commit comments