Skip to content

Commit 759f5a1

Browse files
Merge pull request #90 from SciML/as/symcontainer-recursive
fix: fix stack overflow for methods using `symbolic_container` to define a fallback
2 parents a5be447 + 463e750 commit 759f5a1

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

docs/src/api.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
### Mandatory methods
66

77
```@docs
8-
symbolic_container
98
is_variable
109
variable_index
1110
variable_symbols
@@ -26,6 +25,10 @@ allvariables
2625

2726
### Optional Methods
2827

28+
```@docs
29+
symbolic_container
30+
```
31+
2932
#### Observed equation handling
3033

3134
```@docs

src/index_provider_interface.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ Using `indp`, return an object that implements the index provider interface. In
55
itself implements the interface, `indp` can be returned as-is. All index provider interface
66
methods fall back to calling the same method on `symbolic_container(indp)`, so this may be
77
used for trivial implementations of the interface that forward all calls to another object.
8+
9+
Note that this method is optional. Thus the correct method to check for a fallback is:
10+
```julia
11+
hasmethod(symbolic_container, Tuple{typeof(indp)}) && symbolic_container(indp) != indp
12+
```
813
"""
914
function symbolic_container end
1015

@@ -59,8 +64,9 @@ parameter_index(indp, sym) = parameter_index(symbolic_container(indp), sym)
5964
Check whether the given `sym` is a timeseries parameter in `indp`.
6065
"""
6166
function is_timeseries_parameter(indp, sym)
62-
if hasmethod(symbolic_container, Tuple{typeof(indp)})
63-
is_timeseries_parameter(symbolic_container(indp), sym)
67+
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
68+
(sc = symbolic_container(indp)) != indp
69+
is_timeseries_parameter(sc, sym)
6470
else
6571
return false
6672
end
@@ -91,7 +97,8 @@ parameter in `indp`. Defaults to returning `nothing`. Respects the
9197
[`symbolic_container`](@ref) fallback for `indp` if present.
9298
"""
9399
function timeseries_parameter_index(indp, sym)
94-
if hasmethod(symbolic_container, Tuple{typeof(indp)})
100+
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
101+
(sc = symbolic_container(indp)) != indp
95102
timeseries_parameter_index(symbolic_container(indp), sym)
96103
else
97104
return nothing
@@ -111,7 +118,8 @@ By default, this function returns `nothing`, indicating that the index provider
111118
support generating parameter observed functions.
112119
"""
113120
function parameter_observed(indp, sym)
114-
if hasmethod(symbolic_container, Tuple{typeof(indp)})
121+
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
122+
(sc = symbolic_container(indp)) != indp
115123
return parameter_observed(symbolic_container(indp), sym)
116124
else
117125
return nothing
@@ -143,7 +151,8 @@ variable.
143151
By default, this function returns `Set([ContinuousTimeseries()])`.
144152
"""
145153
function get_all_timeseries_indexes(indp, sym)
146-
if hasmethod(symbolic_container, Tuple{typeof(indp)})
154+
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
155+
(sc = symbolic_container(indp)) != indp
147156
return get_all_timeseries_indexes(symbolic_container(indp), sym)
148157
else
149158
return Set([ContinuousTimeseries()])
@@ -240,7 +249,8 @@ Return a dictionary mapping symbols in the index provider to their default value
240249
This includes parameter symbols. The dictionary must be mutable.
241250
"""
242251
function default_values(indp)
243-
if hasmethod(symbolic_container, Tuple{typeof(indp)})
252+
if hasmethod(symbolic_container, Tuple{typeof(indp)}) &&
253+
(sc = symbolic_container(indp)) != indp
244254
default_values(symbolic_container(indp))
245255
else
246256
Dict()

0 commit comments

Comments
 (0)