Instance of a composite type

Hi!
So I am trying to create an instance of a structure:

struct keypoint
    x
    y
    scale
    angle
    Vector{Any}(VecLength)
end

Now I know the values of all the fields except the last one. I need to initialize the instance of the structure with the known values but for the last field I have to call another function where the data to be generated and then stored in the last field of the instance. Is there a way to get this done in Julia?

I am referring to the tutorials here and here but I guess in both places all the fields of the instance have been initialized at one go.

Thanks!

Will this work for you?

struct keypoint
    x
    y
    scale
    angle
    myVector::Vector{Any}
    function keypoint( _x, _y, _scale, _angle )
        _myVector = myFunction( myArgs )
        new( _x, _y, _scale, _angle, _myVector )
    end
end
1 Like

Apart from the straightforward answer above, there is also Parameters.jl which allows you to do

@with_kw struct keypoint
    x = xdefault
    y = ydefault
    scale = scaledefault
    angle = angledefault
    myvector = myfunction(x,y,scale,angle) # calculate myvector default based on other field values
end

(On a side note, maybe this is just because this is a minimal example, but you should type your fields.)

1 Like