@@ -8,10 +8,18 @@ registered function.
88```julia
99@register foo(x, y)
1010@register goo(x, y::Int) # `y` is not overloaded to take symbolic objects
11+ @register hoo(x, y)::Int # `hoo` returns `Int`
1112```
1213"""
1314macro register (expr, Ts = [Num, Symbolic, Real])
14- @assert expr. head == :call
15+ if expr. head === :(:: )
16+ ret_type = expr. args[2 ]
17+ expr = expr. args[1 ]
18+ else
19+ ret_type = Real
20+ end
21+
22+ @assert expr. head === :call
1523
1624 f = expr. args[1 ]
1725 args = expr. args[2 : end ]
@@ -28,14 +36,17 @@ macro register(expr, Ts = [Num, Symbolic, Real])
2836 name (x:: Symbol ) = :($ value ($ x))
2937 name (x:: Expr ) = ((@assert x. head == :(:: )); :($ value ($ (x. args[1 ]))))
3038
31- Expr (:block ,
32- [quote
33- function $f ($ (setinds (args, symbolic_args, ts)... ))
34- wrap = any (x-> typeof (x) <: Num , tuple ($ (setinds (args, symbolic_args, ts)... ),)) ? Num : identity
35- wrap (Term {Real} ($ f, [$ (map (name, args)... )]))
36- end
37- end
38- for ts in types]. .. ) |> esc
39+ ex = Expr (:block )
40+ for ts in types
41+ push! (ex. args, quote
42+ function $f ($ (setinds (args, symbolic_args, ts)... ))
43+ wrap = any (x-> typeof (x) <: Num , tuple ($ (setinds (args, symbolic_args, ts)... ),)) ? Num : identity
44+ wrap (Term {$ret_type} ($ f, [$ (map (name, args)... )]))
45+ end
46+ end )
47+ end
48+ push! (ex. args, :((:: $typeof ($ promote_symtype))(:: $typeof ($ f), args... ) = $ ret_type))
49+ esc (ex)
3950end
4051
4152# Ensure that Num that get @registered from outside the ModelingToolkit
0 commit comments