James Beezho
James Beezho

Reputation: 491

Julia: Declaring Types of Keyword Arguments with Variables

Im trying to declare a type in my keyword argument of an array of Vectors that is variable dimensions choosen by the user at the start of the program. When I use a variable to declare the dimensions, terminal tells me the variable is unknown. This is not an issue in sub 0.4 Julia as multidimensional arrays can be declared with single dimensional arrays. Ive tried constants and they did not work for me either. Is there a way around this without dumping data to a text file and reading from function.

function dataDoer(len,preConns::Array{Array{Int64,1},(len)})
    println("do stuff here")
end


function main()
    local netDim = (2,2)
    local preConns::Array{Array{Int64,1},length(netDim)} = fill!(Array(Array{Int64,1},netDim),Int64[])

    dataDoer(length(netDim),preConns)
end

main()

ERROR: LoadError: UndefVarError: len not defined

Upvotes: 0

Views: 598

Answers (2)

quinnj
quinnj

Reputation: 1268

I would take a look at the docs for how Arrays are implemented, as they shed some light on what's going on here.

In Array{T,N}, the N doesn't refer to the length or size of an array, but it's dimension. Array can actually have variable length, with the option to push!, append!, and splice! elements in and out. To get the length of an array, you simply do length(A). Hope that helps clarify!

Upvotes: 1

Toivo Henningsson
Toivo Henningsson

Reputation: 2709

You will have to make len a type parameter. (I've renamed it to N below, which is common practice for this kind of parameter.) Then you can do eg

function dataDoer{N}(preConns::Array{Array{Int64,1},N})
    println("do stuff here")
end

Note that N is no longer an argument of the function, but is inferred from the type of preConns.

Upvotes: 1

Related Questions