# How to store \_parameteric\_ functions with JLD2?

**URL:** https://discourse.julialang.org/t/how-to-store-parameteric-functions-with-jld2/131552
**Category:** General Usage
**Created:** [August 12, 2025, 8:37am UTC](https://discourse.julialang.org/t/how-to-store-parameteric-functions-with-jld2/131552 "2025-08-12T08:37:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 12, 2025, 8:37am UTC](https://discourse.julialang.org/t/how-to-store-parameteric-functions-with-jld2/131552/1 "2025-08-12T08:37:14Z")

</div>

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](https://stackoverflow.com/questions/71465185/save-a-function-with-jld2).

However, my problem is when the function is parametric.

E.g.

```julia
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)

```

Loading back this struct will rise an error, as [anonymous functions are not supported in JLD2](https://github.com/JuliaIO/JLD2.jl/issues/258).

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 ?

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 12, 2025, 1:28pm UTC](https://discourse.julialang.org/t/how-to-store-parameteric-functions-with-jld2/131552/2 "2025-08-12T13:28:41Z")

</div>

Soimething on these lines may work in my situation, even if I am not super-happy to use non-local vars in a function…

File `definitions.jl`:

```julia
module Foomodule

export scaling_par, f, Foo 
scaling_par::Float64 = 1.0
f(x) = x/scaling_par 
struct Foo
    a::Int
    b::Function
end

end

```

File `writer.jl`:

```julia
using Pkg
cd(@ __DIR__ )
Pkg.activate(".")
using JLD2
includet("definitions.jl")
using .Foomodule

Foomodule.scaling_par = 0.001 
foo = Foo(1, f)
r = foo.b(2) # 2000
write("scaling_parameter.txt", string(Foomodule.scaling_par))
save("foo.jld2", "foo", foo)

```

File `reader.jl` (on a different Julia session):

```julia
using Pkg
cd(@ __DIR__ )
Pkg.activate(".")
using JLD2

includet("definitions.jl")
using .Foomodule

Foomodule.scaling_par = parse(Float64,readline("scaling_parameter.txt")) 
foo2 = load("foo.jld2", "foo")
r2 = foo2.b(2) # 2000

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 12, 2025, 2:47pm UTC](https://discourse.julialang.org/t/how-to-store-parameteric-functions-with-jld2/131552/3 "2025-08-12T14:47:44Z")

</div>

> [@sylvaticus](#):
>
> `foo = Foo(1, x->f(x,0.01))`

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)`.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 12, 2025, 3:08pm UTC](https://discourse.julialang.org/t/how-to-store-parameteric-functions-with-jld2/131552/4 "2025-08-12T15:08:02Z")

</div>

Thank you. This works and simplify my code:

Writer:

```julia
using Pkg
cd(@ __DIR__ )
Pkg.activate(".")
using JLD2

scaling_par::Float64 = 1.0
f(x,scaling_par) = x/scaling_par 
struct Foo
    a::Int
    b::Function
end
foo = Foo(1, Base.Fix2(f, 0.001))
r = foo.b(2) # 2000
save("foo.jld2", "foo", foo)

```

Reader (after julia restart):

```julia
using Pkg
cd(@ __DIR__ )
Pkg.activate(".")
using JLD2

f(x,scaling_par) = x/scaling_par 
struct Foo
    a::Int
    b::Function
end

foo2 = load("foo.jld2", "foo")
r2 = foo2.b(2) # 2000

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [August 12, 2025, 3:14pm UTC](https://discourse.julialang.org/t/how-to-store-parameteric-functions-with-jld2/131552/5 "2025-08-12T15:14:45Z")

</div>

> [@sylvaticus](#):
>
> ```julia-auto
> struct Foo
> a::Int
> b::Function
> end
> 
> ```

I think, this should be parametrized over `b::F` if performance is important

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 13, 2025, 5:46am UTC](https://discourse.julialang.org/t/how-to-store-parameteric-functions-with-jld2/131552/6 "2025-08-13T05:46:03Z")

</div>

Thank you. Indeed it is in the [actual implementation](https://github.com/sylvaticus/BetaML.jl/blob/b3b852f21e0a761bcce6e91cf8506361a4f3a6c2/src/Nn/default_layers/VectorFunctionLayer.jl#L4)🙂
