stone stone
stone stone

Reputation: 95

how to use the "new(....)" inside of a type definition in Julia?

I'm reading a piece of code, and get confused about the use of the new method inside of the type declaration.

After searching the internet, I understand the meaning of inner constructor and outer constructor, and I understand this is the primary use of the new method in Julia, following this link:What does `new(...)` do in Julia?

type GenConfig
    outputPath::String
    mode::String # "all" or "calls"

    # output file names
    ambsFilename::String
    arcsFilename::String
    callsFilename::String
    hospitalsFilename::String
    mapFilename::String
    nodesFilename::String
    prioritiesFilename::String
    stationsFilename::String
    travelFilename::String

    # counts
    numAmbs::Int
    numCalls::Int
    numHospitals::Int
    numStations::Int

    # graph
    xNodes::Int # number of nodes in x direction
    yNodes::Int # number of nodes in y direction

    # map
    map::Map
    mapTrim::Float # fraction of map border to trim, to make sure   objects generated on map are inside borders

    # misc
    startTime::Float
    targetResponseTime::Float
    offRoadSpeed::Float
    stationCapacity::Int
    travelModeSpeeds::Vector{Float}

    # call density raster
    callDensityRasterFilename::String
    cropRaster::Bool
    callRasterCellSeed::Int # seed for rng, will generate raster cell index
    callRasterCellLocSeed::Int # seed for rng, will generate location within raster cell

    # call related distributions and random number generators
    interarrivalTimeDistrRng::DistrRng
    priorityDistrRng::DistrRng
    dispatchDelayDistrRng::DistrRng
    onSceneDurationDistrRng::DistrRng
    transferDistrRng::DistrRng
    transferDurationDistrRng::DistrRng

    # misc RNGs
    ambStationRng::AbstractRNG
    callLocRng::AbstractRNG
    hospitalLocRng::AbstractRNG
    stationLocRng::AbstractRNG

    travelTimeFactorDistrRng::DistrRng

    GenConfig() = new("", "",
        "", "", "", "", "", "", "", "", "",
        nullIndex, nullIndex, nullIndex, nullIndex,
        nullIndex, nullIndex,
        Map(), 1e-6,
        nullTime, nullTime, nullTime, nullIndex, [],
        "", false, nullIndex, nullIndex)
end

My confusion is mainly as the following. The number of values provided in the new method is less then the number of fields in the type declaration. Could anyone explain this to me?

Upvotes: 3

Views: 262

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69949

You can call new with fewer arguments than number of fields to get an incompletely initialized object.

This is intentional as explained in the Incomplete initialization section of the Julia manual.

This is useful for mutable structs when (citing the manual):

The inner constructor method can pass incomplete objects to other functions to delegate their completion:

julia> mutable struct Lazy
           data
           Lazy(v) = complete_me(new(), v)
       end

Upvotes: 3

Related Questions