# User defined functions in a module

**URL:** <https://discourse.julialang.org/t/user-defined-functions-in-a-module/75971>\
**Category:** New to Julia\
**Created:** [February 7, 2022, 7:57pm UTC](https://discourse.julialang.org/t/user-defined-functions-in-a-module/75971 "2022-02-07T19:57:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)\
**Post date:** [February 7, 2022, 7:57pm UTC](https://discourse.julialang.org/t/user-defined-functions-in-a-module/75971/1 "2022-02-07T19:57:39Z")

</div>

Howdy, folks.  
I have a code which allows for a user defined function. I do this with a struct:

```julia
Base.@kwdef mutable struct Solver{T<:Function}
    Δt :: Float64 = 0.3
    t :: Float64 = 0.0
    ...
    kinematicVelocity :: T = (_...) -> SA_F64[0,0,0]
end

```

So the user can create an arbitrary function and put it in that struct, which is used throughout the code and is its main input.  
This works ok, but I don’t love it as a solution. It’s ok when I’m using the CPU version of my code, but for the GPU I don’t really know how to pass an arbitrary function within a struct do it.  
I need to make another one of these user defined functions and I’m wondering about the best way to do it (this function needs to work on the GPU, unlike that other function). One option I thought of was to pass it as a string within that struct and `eval` it in my code.  
The other option I can think of is to let the user simply declare that function before calling my code, overriding my default one. Not sure there are some caveats to doing that.

Has anyone else run into this before? Any clever solutions that I haven’t thought of?  
Thanks a lot!

---

<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:** [February 7, 2022, 8:54pm UTC](https://discourse.julialang.org/t/user-defined-functions-in-a-module/75971/2 "2022-02-07T20:54:12Z")

</div>

Yes, it’s not very idiomatic to have a function as part of a struct.  
You could have

```julia
abstract type SolverAlgorithm end

struct DefaultSolverAlgorithm :< SolverAlgorithm
     Δt :: Float64
       t :: Float64 
     ...
end
function solve(args....,solver::SolverAlgorithm)
    error("Solve not implemented for this solverAlgorithm!")
end
function solve(args....,solver::DefaultSolverAlgorithm=DefaultSolverAlgorithm(Δt=0.3,t=0.0,...)
  ...
end

```

At that point one that want to create a custom solver just need to create a struct child of SolverAlgorithm and provide its own implementation of `solve`.  
This is the way I did implement `train` and `singleUpdate!` in a [deep learning lib](https://github.com/sylvaticus/BetaML.jl/blob/297e870a220198946f1f1659b403606dca193f55/src/Nn/Nn.jl#L703).
