I think what I wrote above might be too complicated, as it is an attempt to
solve this problem.
In essence this is what I want:
function function1(a, b, onGreaterThanCallback)
if(a > b)
c = a + b
res = onGreaterThanCallback(c, z)
return res + 1
else
# do anything
# but return nothing
end
end
global onGreaterThanCallback = (c) -> c + z
function1(a, b, onGreaterThanCallback)
Problems:
The global variable.
The anonymous function which has performance impact (vs other approaches).
We could use Tim Holy's @anon, but then the value of `z` is fixed at
function definition, which we don't always want.
I think that the ideal optimization would look like this:
function function1(a, b, z) # Variable needed in callback fun
injected.
if(a > b)
c = a + b
res = c + z # Callback function has been injected.
return res + 1
else
# do anything
# but return nothing
end
end
function1(a, b, z)
In OO languages we would be using an abstract class or its equivalent. But
I've thought about it, and read the discussions on interfaces, and don't
see those solutions optimizing the code out like I did above.
Any ideas?