Skip to content

Commit 1c45ee2

Browse files
committed
I don't see how to get this approach to work.
I need to evaluate the unit macro to know the types to put in the model definition. I need a type parameter to be generic over Unitful units or DynamicQuantities Dimensions, but that can't exist until after evaluation.
1 parent 6f4b590 commit 1c45ee2

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/systems/model_parsing.jl

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,33 @@ end
142142

143143
pop_structure_dict!(dict, key) = length(dict[key]) == 0 && pop!(dict, key)
144144

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+
145168
function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
146169
varclass, where_types, meta)
147170
if indices isa Nothing
148-
kwtype = if !isnothing(meta) && haskey(meta, VariableUnit)
149-
dim = dimension(eval(meta[VariableUnit]))
150-
units = gensym(:U)
151-
push!(where_types, units)
152-
Expr(:curly, Union, :Nothing, Expr(:curly, Quantity, Expr(:(<:), type), dim, units))
153-
else
154-
Expr(:curly, Union, Nothing, type)
155-
end
171+
kwtype = create_kwarg_type(meta, where_types, type)
156172
push!(kwargs, Expr(:kw, Expr(:(::), a, kwtype), nothing))
157173
dict[:kwargs][getname(var)] = Dict(:value => def, :type => type)
158174
else

0 commit comments

Comments
 (0)