# How to access other field variables?

**URL:** https://discourse.julialang.org/t/how-to-access-other-field-variables/34254
**Category:** New to Julia
**Created:** [February 6, 2020, 3:35pm UTC](https://discourse.julialang.org/t/how-to-access-other-field-variables/34254 "2020-02-06T15:35:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 6, 2020, 3:35pm UTC](https://discourse.julialang.org/t/how-to-access-other-field-variables/34254/1 "2020-02-06T15:35:01Z")

</div>

I have a composite type which as a field of type `Function`, how could this function access other fields of struct?

```julia
struct Adder
    adds::Vector{Float64}
    f::Function

   Adder(v::AbstractVector{Float64}) = new(v,
                                           (x, i) -> x + adds[i] )
end

julia> obj = Adder([1.0, 2.0] )
Adder([1.0, 2.0], var"#5#6"())

julia> obj.f.([10.0, 20.0], 1:2)
ERROR: UndefVarError: adds not defined

```

to be concrete, in the above MWE, how could `f()` access the field variable `adds`? thanks.

---

<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: [February 6, 2020, 3:39pm UTC](https://discourse.julialang.org/t/how-to-access-other-field-variables/34254/2 "2020-02-06T15:39:58Z")

</div>

You have to pass the `adds` object (or other fields) as an argument to `f`, or capture the fields in a closure via something like `(x, i) -> x + v[i]` in the constructor.

```julia
struct Adder
    adds::Vector{Float64}
    f::Function

    function Adder(v::AbstractVector)
        a = Vector{Float64}(v) # perform type conversion if needed
        new(a, (x, i) -> x + a[i])
    end
end

```

**However** , it sounds like you are trying to emulate object-oriented-programming (OOP) syntax in Julia. This will result in non-idiomatic code and poor performance, so as a general rule I would avoid this style. In particular, instead of `object.func(args...)` syntax as in OOP, in Julia you might do `func(object, args...)`, and define a method of `func` that takes an parameter of your object type as the first argument.

---

<div class="post-metadata">

### Author: ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)
#### Post date: [February 6, 2020, 3:48pm UTC](https://discourse.julialang.org/t/how-to-access-other-field-variables/34254/3 "2020-02-06T15:48:43Z")

</div>

> [@stevengj](#):
>
> This will result in non-idiomatic code and poor performance

in your modified code, why and how could it give poor performance?  
thanks.

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [February 6, 2020, 6:03pm UTC](https://discourse.julialang.org/t/how-to-access-other-field-variables/34254/4 "2020-02-06T18:03:42Z")

</div>

> [@tomtom](#):
>
> in your modified code, why and how could it give poor performance?  
> thanks.

In Steven’s particular example, I don’t think there is a performance problem, but in many cases doing things like this you are liable to run into [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276)

---

<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: [February 6, 2020, 6:06pm UTC](https://discourse.julialang.org/t/how-to-access-other-field-variables/34254/5 "2020-02-06T18:06:07Z")

</div>

> [@tomtom](#):
>
> in your modified code, why and how could it give poor performance?

Because your `struct` has an [abstractly typed field](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-fields-with-abstract-type-1) (`::Function`). So for every call to `f` the compiler has to figure out the return type at runtime, it can’t inline it, etc. etc.

---

<div class="post-metadata">

### Author: ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)
#### Post date: [February 6, 2020, 6:43pm UTC](https://discourse.julialang.org/t/how-to-access-other-field-variables/34254/6 "2020-02-06T18:43:24Z")

</div>

You could make `Adder` itself [callable](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects-1):

```julia
julia> struct Adder
           adds::Vector{Float64}
       end

julia> (A::Adder)(x,i) = x + A.adds[i]

julia> obj = Adder([1.0, 2.0])
Adder([1.0, 2.0])

julia> obj([10.0, 20.0], 1:2)
2-element Array{Float64,1}:
 11.0
 22.0

```

That seems more idiomatic to me, and avoids the performance problem pointed out by @stevengj.
