Thanks again for the inputs!
It let me to the following solution:
## ---
module mymodule
function make_foo(f::Function)
!method_exists(f, (Real,)) ? error("provide function with 'Real'
argument.") : nothing
## create new methods
foo(x::Real) = f(x::Real)
function foo(x
The module might also export a macro that you can apply to the method
declaration.
I think that design is flawed – you don't want to be adding random methods
to user-supplied functions. You could just use a helper function in the
definition of bar that applies the foo transformation using a user-supplied
anonymous function applied to each scalar.
On Thu, Mar 27, 2014 at 10:26 A
Thanks for the fast response!
However, when I change bar as proposed in the module this gives an error
"No method foo(Array)":
foo(x::Real) = x^2
bar(foo, [1:3])
As far I understand passing foo as function argument to bar(f, y) is not
sufficient. The problem is that bar() uses foo(x::Real) a
Thanks for the fast response!
However, when I change bar in the module this gives an error "No method
foo(Array)":
foo(x::Real) = x^2
println(bar(foo, [1:3]))
As far I understand passing foo as function argument to bar(f, y) is not
sufficient. The problem is that bar() uses
Aka function arguments – I've been doing too much wrapping of C libraries
lately. Example:
function bar(f,y)
a = length(y)
[f(a) f(y)]
end
Which can be used like this:
bar(y) do x
# body of f(x)
end
You can also pass an explicit function argument as bar(f,y). (I swear this
email wa
Callbacks?
On Thu, Mar 27, 2014 at 8:04 AM, andreas wrote:
>
> Dear list,
>
> I'd like to be able to "pass" a function to a module, lets say
> foo(x::Real). foo(x::Real) is used in the module and more methods are
> added (which make use of foo(x::Real)).
>
> My idea is that the user loads the
Dear list,
I'd like to be able to "pass" a function to a module, lets say foo(x::Real).
foo(x::Real) is used in the module and more methods are added (which make
use of foo(x::Real)).
My idea is that the user loads the module and can then define foo(x::Real).
If the user later redefines this