Fill an instance of a function with a "." syntax (e.g. mod.fit(x, y))

Dear all

In the simplified example below, I have the following syntax where I build an instance of a given function “foo”. I named this instance “mod” (but this name can vary) and, in this instance, I fill the sub-object “res” (empty at start) by a function fit!:

Base.@kwdef mutable struct Foo{
        FUN <: Function,
        RES
        }
    fun::FUN
    res::Union{Nothing, RES}
end
function fit!(mod::Foo, x, y)
    mod.res = mod.fun(x, y)
end  
foo() = Foo{Function, Number}(+, nothing)

## Build an instance "mod" 
mod = foo() ;
fit!(mod, 2, 3) ;
mod.res
julia > 5

What is above works. But I was asking if, instead this syntax, it was simple or not (and keeping efficiency) to get a syntax such as below and doing the same:

Base.@kwdef mutable struct Foo{
        FUN <: Function,
        FIT <: Function,
        RES
        }
    fun::FUN
    fit::FUN
    res::Union{Nothing, RES}
end
## ?: fit = ... 
## ?:  foo() = Foo{Function, Function, Number}(+, fit, nothing)
## Build an instance "mod"
## and expected syntax 
mod = foo()
mod.fit(2, 3) ;   # here this should fill "mod.res" with 2 + 3
mod.res
julia > 5

I tried but was not successful.
If you have examples on how to manage this, I am interested.
Thanks

You probably would need a package that emulates object oriented code in Julia, like, for example: GitHub - Suzhou-Tongyuan/ObjectOriented.jl: Conventional object-oriented programming in Julia without breaking Julia's core design ideas.

But the first option you posted is the most idiomatic way to use Julia.

1 Like

Thanks for your answer, it agrees with what I was believing, thanks for the confirmation and the info on ObjectOriented.jl.