Suppose I have some nested functions, and the lowest level function has some arguments which are caches and/or outputs whose values need to be written to and retained between calls:
function a!(arg1, arg2)
b!(arg1, arg2)
return nothing
end
function b!(a1, a2)
c!(a1, a2)
return nothing
end
function c!(a1, a2)
a1[:] = <something>
...
return nothing
end
mem1 = <something>
mem2 = <something else>
a!(mem1, mem2)
In the above, it’s clear that a!
and b!
need to have the arguments to c!
in their call signatures so that they can be passed down. This means that if I want to change c!
to be a different function, I have to change a!
and b!
as well, which is cumbersome if c!
takes a lot of arguments or if I want to try many different functions.
Suppose instead I do the following:
function a!()
b!()
return nothing
end
function b!()
c!()
return nothing
end
function f!(a1, a2)
a1[:] = <something>
...
return nothing
end
mem1 = <something>
mem2 = <something else>
c!() = f!(mem1, mem2)
a!()
In this case, a!
and b!
don’t need to know anything about the arguments to c!
, which makes it easier to redefine c!
to be different things. Is there a reason I shouldn’t do this, particularly in terms of performance (it is the case that in my application, a!
will be called many times)? I’d also happily welcome any other suggestions to achieve this goal of not having to pass the arguments down in the first way (I thought about using some kind of data structure to encapsulate all the arguments to c!
then pass that down, but it’s also important that these functions are able to be compiled to a GPU kernel, so any argument that is not isbits
won’t work).
Thank you for your help!