Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

How to access other field variables?

$
0
0

@tomtom wrote:

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

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.

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles