I have a struct (part of a ML model in the real case) where one field is a function.
For the function itself, as the function is provided by the package, I have no issues saving and loading it back.
However, my problem is when the function is parametric.
E.g.
using JLD2
scaling_par= 0.1
f(x,scaling_par=scaling_par) = x/scaling_par # this is a small function can can be exported and made available by my package
struct Foo
a::Int
b::Function
end
foo = Foo(1, x->f(x,0.01))
r = foo.b(2)
save("foo.jld2", "foo", foo)
I don’t really have much control on the struct Foo, but I can save into it and use it as I prefer. Which alternative approach may I use to store and use also the information about the scaling parameter ?
If you need to save it, you could use a more explicit data structure instead of an anonymous function. In this case, Base provides such a type for you: you can replace x->f(x,0.01) with Base.Fix2(f, 0.01).