Best coding of homogeneous types

Hi,

I have a code where there are several Float64 type variables, which I would like to group as:

Group1: H_0, Omega_m0, Omega_Lambda0
Group2: Omega_geom0, A, z_f, z_t

These groups, Group1 and Group2, will be used as arguments to certain functions and I would like to call their variables by the above names (H_0, …, Omega_geom0, …), not (only) by their indices. Thus I would like some suggestions as to which intrinsic Julia types to use, for better performance and convenience: named array (is there such a thing?), named tuple, dictionary, struct or whatever?

Thanks in advance

I’d suggest named args for the functions, named tuples to group the values and splatting to pass the arguments:

group1 = (H_0 = H_0, Omega_m0 = Omega_m0, Omega_Lambda0 = Omega_Lambda0)

function myfunc(x, t; H_0, Omega_m0, Omega_Lambda0)
    # body
end

myfunc(x, t; group1...)

Also, IIRC, in 1.5

myfunc(x, t; H_0, Omega_m0, Omega_Lambda0)
#          ^ note the semicolon

is the same as

myfunc(x, t, H_0=H_0, Omega_m0=Omega_m0, Omega_Lambda0=Omega_Lambda0)

So, you may choose to just have the appropriate variable names in the call scope without much need to group them into a structure.

I would definitely recommend a struct or mutable struct, which you can learn more about here: Types · The Julia Language

A named tuple will also work, but a struct gives you the opportunity to name that collection of data in some meaningful way, and it makes it easy to create functions that operate on that particular collection of data.

2 Likes