How to define a Fortran type equivalent object in Julia

I have a fortran type I defined as mean_covar like below,

type :: mean_covar
    real(kind=r8), allocatable :: mu(:,:)
    real(kind=r8), allocatable :: sigma(:,:)
    real(kind=r8) :: w
end type

So that I can set some variable as type Mean_covar.

I want to define the same thing in Julia, I know Julia has something called struct, with mutable property its fields can be changed. So I did a very simply thing like,

mutable struct Mean_covar
mu::Array{Float64,2}
sigma::Array{Float64,2}
w::Int64  
end

Then I define,

musigma = Mean_covar(Array{Float64,2}(undef,2,2),Array{Float64,2}(undef,2,2), 0.0)

so I can then set musigma.mu as any 2d array I want.

But I just wonder, does it has to be so cumbersome when defining musigma (need to set the value of each fields)? Can I just simply do

musigma::Mean_covar

But this give me an error,

UndefVarError: musigma not defined

Thank you very much!

you can write a constrictor that by default gives you an object with uninitiated arrays, so you don’t have to pass the three variables manually everytime you make a new one.

2 Likes

Parameters.jl also does that automatically.

1 Like