On Sunday, January 31, 2016 at 3:47:03 AM UTC-5, Bryan Rivera wrote: > > What do you mean "I would favor using a regular function call with a > descriptive name"? >
> I was thinking replace `Base.call` with a function name. > Yes, that's what I meant. It depends on the context, sometimes "function application" is the most natural way to do it. But that does not work if the function is in the top-level scope in > another file. > It will work if you share the same function through `using` and `import ThatModule.function_name` directives. Check out the docs. Cédric > > On Thursday, January 21, 2016 at 9:56:51 PM UTC-5, Cedric St-Jean wrote: >> >> Something like this? >> >> function function1(a, b, f) # Variable needed in callback fun injected. >> if(a > b) >> c = a + b >> res = f(c) # Callback function has been injected. >> return res + 1 >> else >> # do anything >> # but return nothing >> end >> end >> >> type SomeCallBack >> z::Int >> end >> Base.call(callback::SomeCallBack, c) = c + callback.z >> >> function1(2, 1, SomeCallBack(10)) >> >> Because of JIT, this is 100% equivalent to your "callback function has >> been injected" example, performance-wise. My feeling is that .call >> overloading is not to be abused in Julia, so I would favor using a regular >> function call with a descriptive name instead of call overloading, but the >> same performance guarantees apply. Does that answer your question? >> >> On Thursday, January 21, 2016 at 9:02:50 PM UTC-5, Bryan Rivera wrote: >>> >>> 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? >>> >>
