3535
3636function _model_macro (mod, name, expr, isconnector)
3737 exprs = Expr (:block )
38- dict = Dict {Symbol, Any} ()
39- dict[:kwargs ] = Dict {Symbol, Any} ()
38+ dict = Dict {Symbol, Any} (
39+ :kwargs => Dict {Symbol, Dict} (),
40+ :structural_parameters => Dict {Symbol, Dict} ()
41+ )
4042 comps = Symbol[]
4143 ext = Ref {Any} (nothing )
4244 eqs = Expr[]
@@ -107,7 +109,8 @@ function _model_macro(mod, name, expr, isconnector)
107109end
108110
109111function parse_variable_def! (dict, mod, arg, varclass, kwargs;
110- def = nothing , indices:: Union{Vector{UnitRange{Int}}, Nothing} = nothing )
112+ def = nothing , indices:: Union{Vector{UnitRange{Int}}, Nothing} = nothing ,
113+ type:: Union{Type, Nothing} = nothing )
111114 metatypes = [(:connection_type , VariableConnectType),
112115 (:description , VariableDescription),
113116 (:unit , VariableUnit),
@@ -125,15 +128,34 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
125128 arg isa LineNumberNode && return
126129 MLStyle. @match arg begin
127130 a:: Symbol => begin
128- push! (kwargs, Expr (:kw , a, nothing ))
131+ if type isa Nothing
132+ push! (kwargs, Expr (:kw , a, nothing ))
133+ else
134+ push! (kwargs, Expr (:kw , Expr (:(:: ), a, Union{Nothing, type}), nothing ))
135+ end
129136 var = generate_var! (dict, a, varclass; indices)
130- dict[:kwargs ][getname (var)] = def
137+ dict[:kwargs ][getname (var)] = Dict ( :value => def, :type => type)
131138 (var, def)
132139 end
140+ Expr (:(:: ), a, type) => begin
141+ type = Core. eval (mod, type)
142+ _type_check! (a, type)
143+ parse_variable_def! (dict, mod, a, varclass, kwargs; def, type)
144+ end
145+ Expr (:(:: ), Expr (:call , a, b), type) => begin
146+ type = Core. eval (mod, type)
147+ def = _type_check! (def, a, type)
148+ parse_variable_def! (dict, mod, a, varclass, kwargs; def, type)
149+ end
133150 Expr (:call , a, b) => begin
134- push! (kwargs, Expr (:kw , a, nothing ))
151+ if type isa Nothing
152+ push! (kwargs, Expr (:kw , a, nothing ))
153+ else
154+ push! (kwargs, Expr (:kw , Expr (:(:: ), a, Union{Nothing, type}), nothing ))
155+ end
135156 var = generate_var! (dict, a, b, varclass; indices)
136- dict[:kwargs ][getname (var)] = def
157+ type != = nothing && (dict[varclass][getname (var)][:type ] = type)
158+ dict[:kwargs ][getname (var)] = Dict (:value => def, :type => type)
137159 (var, def)
138160 end
139161 Expr (:(= ), a, b) => begin
@@ -304,15 +326,23 @@ function parse_structural_parameters!(exprs, sps, dict, mod, body, kwargs)
304326 Base. remove_linenums! (body)
305327 for arg in body. args
306328 MLStyle. @match arg begin
329+ Expr (:(= ), Expr (:(:: ), a, type), b) => begin
330+ type = Core. eval (mod, type)
331+ b = _type_check! (Core. eval (mod, b), a, type)
332+ push! (sps, a)
333+ push! (kwargs, Expr (:kw , Expr (:(:: ), a, type), b))
334+ dict[:structural_parameters ][a] = dict[:kwargs ][a] = Dict (
335+ :value => b, :type => type)
336+ end
307337 Expr (:(= ), a, b) => begin
308338 push! (sps, a)
309339 push! (kwargs, Expr (:kw , a, b))
310- dict[:kwargs ][a] = b
340+ dict[:structural_parameters ][a] = dict[ : kwargs ][a] = Dict ( :value => b)
311341 end
312342 a => begin
313343 push! (sps, a)
314344 push! (kwargs, a)
315- dict[:kwargs ][a] = nothing
345+ dict[:structural_parameters ][a] = dict[ : kwargs ][a] = Dict ( :value => nothing )
316346 end
317347 end
318348 end
@@ -336,17 +366,17 @@ function extend_args!(a, b, dict, expr, kwargs, varexpr, has_param = false)
336366 end
337367 end
338368 push! (kwargs, Expr (:kw , x, nothing ))
339- dict[:kwargs ][x] = nothing
369+ dict[:kwargs ][x] = Dict ( :value => nothing )
340370 end
341371 Expr (:kw , x) => begin
342372 push! (kwargs, Expr (:kw , x, nothing ))
343- dict[:kwargs ][x] = nothing
373+ dict[:kwargs ][x] = Dict ( :value => nothing )
344374 end
345375 Expr (:kw , x, y) => begin
346376 b. args[i] = Expr (:kw , x, x)
347377 push! (varexpr. args, :($ x = $ x === nothing ? $ y : $ x))
348378 push! (kwargs, Expr (:kw , x, nothing ))
349- dict[:kwargs ][x] = nothing
379+ dict[:kwargs ][x] = Dict ( :value => nothing )
350380 end
351381 Expr (:parameters , x... ) => begin
352382 has_param = true
@@ -851,3 +881,18 @@ function parse_conditional_model_statements(comps, dict, eqs, exprs, kwargs, mod
851881 $ equations_blk
852882 end ))
853883end
884+
885+ _type_check! (a, type) = return
886+ function _type_check! (val, a, type)
887+ if val isa type
888+ return val
889+ else
890+ try
891+ return convert (type, val)
892+ catch
893+ (e)
894+ throw (TypeError (Symbol (" `@mtkmodel`" ),
895+ " `@structural_parameters`, while assigning to `$a `" , type, typeof (val)))
896+ end
897+ end
898+ end
0 commit comments