Darrell123
Darrell123

Reputation: 53

Incomplete Initialisation in Julia

I want to initialise part of a Datatype in Julia, I currently have:

mutable struct Foo
    bar::Int
    baz::Int
    maz::Int
    Foo(maz=2)=new(maz)
end

foo=Foo()
println(foo)

However this creates an object which is Foo(2, 0, 0)

How do I do so that I get Foo(0, 0, 2)

Note: I would prefer to not have to do a complete initialisation

Upvotes: 3

Views: 599

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69899

Use new() (see here for details):

julia> mutable struct Foo
           bar::Int
           baz::Int
           maz::Int
           function Foo(maz=2)
               foo = new()
               foo.maz = maz
               return foo
           end
       end

julia> foo=Foo()
Foo(139985835568976, 8, 2)

Note that bar and baz fields are not set to 0 but uninitialized.

Upvotes: 4

Related Questions