# Proper way to identify variables/models with functions

**URL:** <https://discourse.julialang.org/t/proper-way-to-identify-variables-models-with-functions/94535>\
**Category:** General Usage\
**Created:** [February 13, 2023, 3:39am UTC](https://discourse.julialang.org/t/proper-way-to-identify-variables-models-with-functions/94535 "2023-02-13T03:39:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PaterPen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paterpen/32/27985_2.png) [@PaterPen](https://discourse.julialang.org/u/PaterPen)\
**Post date:** [February 13, 2023, 3:39am UTC](https://discourse.julialang.org/t/proper-way-to-identify-variables-models-with-functions/94535/1 "2023-02-13T03:39:35Z")

</div>

Recently I revisited some old Julia code of mine and wondered how to implement it properly.

The setting is: I have a `model` struct which holds some model definition. Let’s say, each model just describes some system of equations which we want to solve for all variables `x[i]`:

```julia
struct model
    equations::String # just to simplify (no strings in reality)
end

a = model("x[1] = x[2] and x[2] = 0")
b = model("x[1] = x[2] + 1 and x[2] = 0")

```

Therefore, I want to have a function `solve(foo::model)` which returns `[0, 0]` for `a` and `[1, 0]` in the case of `b`. For that reason, I have some macro which autogenerates the functions

```julia
function f_a!(F, x)
    F[1] = x[2] - x[1]
    F[2] = -x[2]
end

```

and

```julia
function f_b!(F, x)
    F[1] = x[2] + 1 - x[1]
    F[2] = -x[2]
end

```

These allow us to formulate the model problem as F(x) = 0 and we can plug the respective function into NLsolve.jl.

My simple question is now: How do we properly identify the model `foo` with the function `f_foo!` inside the function `solve`? After all, all models have the same type. My first intention is to just add some field `id` or `name` which can then be used inside `solve`. However, assume we have 1000 models loaded, then this is associated with runtime costs.

We could also have a new field `f!::Function` in every model which holds the correct function. This seems kinda odd.

Furthermore, if I define a new type for every model instead, it is not a good user experience. Of course I could hide them with some kind of composition:

```julia
struct model
    equations <: AbstractEquations
end

```

and then have the structs `Equations1`, `Equations2` etc. for each new model.  
But I guess I am overthinking this way too much. Any suggestions?

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [February 13, 2023, 8:03am UTC](https://discourse.julialang.org/t/proper-way-to-identify-variables-models-with-functions/94535/2 "2023-02-13T08:03:37Z")

</div>

> [@PaterPen](#):
>
> We could also have a new field `f!::Function` in every model which holds the correct function. This seems kinda odd.

Seems like a reasonable approach if you want to create many independent objects, so not rely on dispatch with lots of types.

It’s also what, e.g., SciML does to define their problem types, so I don’t think it’s _that_ odd 🙂

> <https://github.com/SciML/SciMLBase.jl/blob/552eb96c5d27e999dd866f1999a191f5ec884774/src/problems/basic_problems.jl#L229>

But (as in the example) it’s probably better to use the concrete type of the model function as a parameter, instead of `f!::Function` (otherwise it will be an abstract type for the field, which is usually worse than a concretely typed field).
