Calling a function with 2 arguments of type Float64 and a struct, via its dotted version

The problem happens because Julia is trying to broadcast over cosmo as if it were some kind of array or other container (that’s the default behavior of broadcasting). To tell Julia to treat cosmo as a scalar, you can do:

E_Hubble.(z, Ref(cosmo))

where the Ref type acts as an instruction to treat its argument as a scalar.

To tell Julia to treat all Cosmo_model instances as scalars in broadcasting, you can do:

Base.broadcastable(s::Cosmo_model) = Ref(s)

and then E_Hubble.(z, cosmo) will just work. In other words, this tells Julia that any Cosmo_model should always be wrapped in a Ref (and thus treated as a scalar) automatically when broadcasting.

Your code looks fine, but it will be easier to read if you can quote your code so that it is formatted properly.

One thing I would mention is that structs in Julia are almost always written LikeThis instead of Like_this, so I would have done:

@with_kw struct CosmoModel
  ...
end

As for using Parameters.jl, it’s a really great tool, and there’s no reason not to use it if it fits your needs. One of the most important features of Julia is the fact that “core” Julia code is not inherently faster or better than anything you can write in your own code or find in a package.

5 Likes