# Parametrizing a self-referential function field

**URL:** <https://discourse.julialang.org/t/parametrizing-a-self-referential-function-field/44538>\
**Category:** General Usage\
**Tags:** parametric-types, struct, self-reference\
**Created:** [August 8, 2020, 1:56am UTC](https://discourse.julialang.org/t/parametrizing-a-self-referential-function-field/44538 "2020-08-08T01:56:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [August 8, 2020, 1:56am UTC](https://discourse.julialang.org/t/parametrizing-a-self-referential-function-field/44538/1 "2020-08-08T01:56:27Z")

</div>

My struct changes its input function to automatically get the struct as an argument. This works if I don’t type the struct, but I have no idea how I can type it.

```julia
mutable struct CoronaModel{F1}
    # other fields omitted
    nextSickness::F1
    function CoronaModel{F1}(nextSickness) where {F1}
        me = new()
        me.nextSickness = (p) -> nextSickness(me, p)
        me
    end
end

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [August 8, 2020, 7:46am UTC](https://discourse.julialang.org/t/parametrizing-a-self-referential-function-field/44538/2 "2020-08-08T07:46:05Z")

</div>

I’m not sure what you’re trying to do here, so I may be way off.

But you seem to want your function to have access to the internal fields of your struct?  
Then maybe consider calling the struct as a function?

```julia
mutable struct CoronaModel
    a::Int
    b::Int
end

function (model::CoronaModel)(p)
    return model.a + model.b + p
end

```

Then you can “call” an object `m::CoronaModel` with argument `p`

```julia
julia> m = CoronaModel(2,5)
CoronaModel(2, 5)

julia> m(8)
15

julia> m.a = 3
3

julia> m(8)
16

```

---

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [August 8, 2020, 10:05am UTC](https://discourse.julialang.org/t/parametrizing-a-self-referential-function-field/44538/3 "2020-08-08T10:05:07Z")

</div>

That wouldn’t work, because I want the function “nextSickness” to be itself customizable by the creator of the struct. Different CoronaModel instances will have different “nextSickness” functions. This function will be given with the signature (::CoronaModel,::SthElse) to the constructor of CoronaModel, and the constructor will wrap it around so that it can be called by merely supplying (::SthElse).  
To rephrase; I want the struct to accept a function field that has access to the struct’s fields.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 8, 2020, 11:19am UTC](https://discourse.julialang.org/t/parametrizing-a-self-referential-function-field/44538/4 "2020-08-08T11:19:00Z")

</div>

> [@NightMachinary](#):
>
> To rephrase; I want the struct to accept a function field that has access to the struct’s fields.

I am still not sure what you are trying to do here, but note that there is no Julia equivalent of `self` etc in various OOP languages, so you may just need to use a different approach.

---

<div class="post-metadata">

**Author:** ![NightMachinary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nightmachinary/32/14196_2.png) [@NightMachinary](https://discourse.julialang.org/u/NightMachinary)\
**Post date:** [August 8, 2020, 11:46am UTC](https://discourse.julialang.org/t/parametrizing-a-self-referential-function-field/44538/5 "2020-08-08T11:46:20Z")

</div>

Well, I just dropped parametrizing on that function field for a perf penalty. Otherwise, I can simulate the `self` behavior perfectly. I am content. 🙂
