Skip to content

Commit d6ac059

Browse files
committed
allow different number of out-of-place args for isinplace
1 parent 12f3030 commit d6ac059

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/utils.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,18 @@ function Base.showerror(io::IO, e::FunctionArgumentsError)
216216
end
217217

218218
"""
219-
isinplace(f, inplace_param_number[,fname="f"])
219+
isinplace(f, inplace_param_number, fname = "f", iip_preferred = true;
220+
has_two_dispatches = true,
221+
outofplace_param_number = inplace_param_number - 1)
220222
isinplace(f::AbstractSciMLFunction[, inplace_param_number])
221223
222224
Check whether a function operates in place by comparing its number of arguments
223225
to the expected number. If `f` is an `AbstractSciMLFunction`, then the type
224226
parameter is assumed to be correct and is used. Otherwise `inplace_param_number`
225227
is checked against the methods table, where `inplace_param_number` is the number
226228
of arguments for the in-place dispatch. The out-of-place dispatch is assumed
227-
to have one less. If neither of these dispatches exist, an error is thrown.
229+
to have `outofplace_param_number` parameters (one less than the inplace version
230+
by default). If neither of these dispatches exist, an error is thrown.
228231
If the error is thrown, `fname` is used to tell the user which function has the
229232
incorrect dispatches.
230233
@@ -238,22 +241,23 @@ form is disabled and the 2-argument signature is ensured to be matched.
238241
239242
# See also
240243
241-
- [`numargs`](@ref numargs)
244+
- [`numargs`](@ref numargs)
242245
"""
243246
function isinplace(f, inplace_param_number, fname = "f", iip_preferred = true;
244-
has_two_dispatches = true, isoptimization = false)
247+
has_two_dispatches = true, isoptimization = false,
248+
outofplace_param_number = inplace_param_number - 1)
245249
nargs = numargs(f)
246250
iip_dispatch = any(x -> x == inplace_param_number, nargs)
247-
oop_dispatch = any(x -> x == inplace_param_number - 1, nargs)
251+
oop_dispatch = any(x -> x == outofplace_param_number, nargs)
248252

249253
if length(nargs) == 0
250254
throw(NoMethodsError(fname))
251255
end
252256

253257
if !iip_dispatch && !oop_dispatch && !isoptimization
254-
if all(x -> x > inplace_param_number, nargs)
258+
if all(>(inplace_param_number), nargs)
255259
throw(TooManyArgumentsError(fname, f))
256-
elseif all(x -> x < inplace_param_number - 1, nargs) && has_two_dispatches
260+
elseif all(<(outofplace_param_number), nargs) && has_two_dispatches
257261
# Possible extra safety?
258262
# Find if there's a `f(args...)` dispatch
259263
# If so, no error

test/function_building_error_messages.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ f = Foo{1}()
2424
(this::Foo{T})(args...) where {T} = 1
2525
@test SciMLBase.isinplace(Foo{Int}(), 4)
2626

27+
@testset "isinplace accepts an out-of-place version with different numbers of parameters " begin
28+
f1(u) = 2 * u
29+
@test !isinplace(f1, 2)
30+
@test_throws SciMLBase.TooFewArgumentsError SciMLBase.isinplace(f1, 4)
31+
@test !isinplace(f1, 4; outofplace_param_number = 1)
32+
end
33+
2734
## Problem argument tests
2835

2936
ftoomany(u, p, t, x, y) = 2u

0 commit comments

Comments
 (0)