如何在Julia的类型定义中使用“ new(....)”?

时间:2019-01-04 15:24:20

标签: julia

我正在阅读一段代码,并对类型声明内部使用new方法感到困惑。

在互联网上搜索后,我了解了内部构造函数和外部构造函数的含义,并且我了解这是Julia中新方法的主要用法,其链接如下: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

我的困惑主要如下。新方法中提供的值数量小于类型声明中的字段数量。有人可以向我解释吗?

1 个答案:

答案 0 :(得分:2)

您可以使用少于字段数的参数调用new来获得未完全初始化的对象。

这是故意的,如Julia手册的Incomplete initialization部分所述。

(在引用本手册时)这对于可变结构很有用:

  

然后,内部构造函数方法可以使用不完整的对象,在返回之前完成其初始化。