Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary in constructor for a mutable struct in Julia

Tags:

julia

Is it possible to initialize a mutable struct with a variable which is a dict.I am trying the following:

mutable struct Global
    speciesCnt::Int64
    chromosomeCnt::Int64
    nodeCnt::Int64
    innov_number::Int64
    innovations::Dict{(Int64,Int64),Int64}
    cf::Config
    function Global(cf::Config)
        new(0,0,0,0,Dict{(Int64,Int64),Int64}(),cf) # global dictionary
    end
end

however, when I run it I get the following error:

LoadError: TypeError: in Type, in parameter, expected Type, got Tuple{DataType,DataType}.

Any help is greatly appreciated. I am using Julia v 1.0

like image 223
Yash Bhargava Avatar asked Dec 24 '22 04:12

Yash Bhargava


1 Answers

A proper type signature for your dict is:

Dict{Tuple{Int64,Int64},Int64}

The easiest way to learn how type signatures in Julia look like is to create an object of the desired type and use typeof function to display its type:

julia> typeof(Dict((1,2)=>3))
Dict{Tuple{Int64,Int64},Int64}
like image 85
Bogumił Kamiński Avatar answered Feb 04 '23 04:02

Bogumił Kamiński