@@ -180,46 +180,57 @@ const AnyIndex = Union{Int, Vector{Int}, Tuple, Colon, OrdinalRange}
180
180
# TBD: can it be reduced to this?
181
181
# const AnyIndex = Union{Int, AbstractRange}
182
182
183
+ # Helper function for getindex; throws a MissingException if data is missing, otherwise returns data
184
+ function _missing_data_check (data)
185
+ if data === missing
186
+ throw (MissingException (" Cannot get index; data is missing. You may have tried to access a value that has not yet been computed." ))
187
+ else
188
+ return data
189
+ end
190
+ end
191
+
192
+ # Helper macro used by connector
193
+ macro allow_missing (expr)
194
+ let e = gensym (" e" )
195
+ retexpr = quote
196
+ try
197
+ $ expr
198
+ catch $ e
199
+ if $ e isa MissingException
200
+ missing
201
+ else
202
+ rethrow ($ e)
203
+ end
204
+ end
205
+ end
206
+ return esc (retexpr)
207
+ end
208
+ end
209
+
183
210
#
184
211
# 3b. TimestepVector
185
212
#
186
213
187
214
function Base. getindex (v:: TimestepVector{FixedTimestep{FIRST, STEP}, T} , ts:: FixedTimestep{FIRST, STEP, LAST} ) where {T, FIRST, STEP, LAST}
188
215
data = v. data[ts. t]
189
- if data === missing
190
- error (" Cannot get index; data is missing. You may have tried to access a value that has not yet been computed." )
191
- else
192
- return data
193
- end
216
+ _missing_data_check (data)
194
217
end
195
218
196
219
function Base. getindex (v:: TimestepVector{VariableTimestep{TIMES}, T} , ts:: VariableTimestep{TIMES} ) where {T, TIMES}
197
220
data = v. data[ts. t]
198
- if data === missing
199
- error (" Cannot get index; data is missing. You may have tried to access a value that has not yet been computed." )
200
- else
201
- return data
202
- end
221
+ _missing_data_check (data)
203
222
end
204
223
205
224
function Base. getindex (v:: TimestepVector{FixedTimestep{D_FIRST, STEP}, T} , ts:: FixedTimestep{T_FIRST, STEP, LAST} ) where {T, D_FIRST, T_FIRST, STEP, LAST}
206
225
t = Int (ts. t + (T_FIRST - D_FIRST) / STEP)
207
226
data = v. data[t]
208
- if data === missing
209
- error (" Cannot get index; data is missing. You may have tried to access a value that has not yet been computed." )
210
- else
211
- return data
212
- end
227
+ _missing_data_check (data)
213
228
end
214
229
215
230
function Base. getindex (v:: TimestepVector{VariableTimestep{D_FIRST}, T} , ts:: VariableTimestep{T_FIRST} ) where {T, D_FIRST, T_FIRST}
216
231
t = ts. t + findfirst (isequal (T_FIRST[1 ]), D_FIRST) - 1
217
232
data = v. data[t]
218
- if data === missing
219
- error (" Cannot get index; data is missing. You may have tried to access a value that has not yet been computed." )
220
- else
221
- return data
222
- end
233
+ _missing_data_check (data)
223
234
end
224
235
225
236
# int indexing version supports old-style components and internal functions, not
@@ -274,40 +285,24 @@ Base.lastindex(v::TimestepVector) = length(v)
274
285
275
286
function Base. getindex (mat:: TimestepMatrix{FixedTimestep{FIRST, STEP}, T} , ts:: FixedTimestep{FIRST, STEP, LAST} , i:: AnyIndex ) where {T, FIRST, STEP, LAST}
276
287
data = mat. data[ts. t, i]
277
- if data === missing
278
- error (" Cannot get index; data is missing. You may have tried to access a value that has not yet been computed." )
279
- else
280
- return data
281
- end
288
+ _missing_data_check (data)
282
289
end
283
290
284
291
function Base. getindex (mat:: TimestepMatrix{VariableTimestep{TIMES}, T} , ts:: VariableTimestep{TIMES} , i:: AnyIndex ) where {T, TIMES}
285
292
data = mat. data[ts. t, i]
286
- if data === missing
287
- error (" Cannot get index; data is missing. You may have tried to access a value that has not yet been computed." )
288
- else
289
- return data
290
- end
293
+ _missing_data_check (data)
291
294
end
292
295
293
296
function Base. getindex (mat:: TimestepMatrix{FixedTimestep{D_FIRST, STEP}, T} , ts:: FixedTimestep{T_FIRST, STEP, LAST} , i:: AnyIndex ) where {T, D_FIRST, T_FIRST, STEP, LAST}
294
297
t = Int (ts. t + (T_FIRST - D_FIRST) / STEP)
295
298
data = mat. data[t, i]
296
- if data === missing
297
- error (" Cannot get index; data is missing. You may have tried to access a value that has not yet been computed." )
298
- else
299
- return data
300
- end
299
+ _missing_data_check (data)
301
300
end
302
301
303
302
function Base. getindex (mat:: TimestepMatrix{VariableTimestep{D_FIRST}, T} , ts:: VariableTimestep{T_FIRST} , i:: AnyIndex ) where {T, D_FIRST, T_FIRST}
304
303
t = ts. t + findfirst (isequal (T_FIRST[1 ]), D_FIRST) - 1
305
304
data = mat. data[t, i]
306
- if data === missing
307
- error (" Cannot get index; data is missing. You may have tried to access a value that has not yet been computed." )
308
- else
309
- return data
310
- end
305
+ _missing_data_check (data)
311
306
end
312
307
313
308
# int indexing version supports old-style components and internal functions, not
0 commit comments