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

**URL:** <https://discourse.julialang.org/t/calling-a-function-with-2-arguments-of-type-float64-and-a-struct-via-its-dotted-version/49817>\
**Category:** New to Julia\
**Created:** [November 9, 2020, 1:43am UTC](https://discourse.julialang.org/t/calling-a-function-with-2-arguments-of-type-float64-and-a-struct-via-its-dotted-version/49817 "2020-11-09T01:43:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [November 9, 2020, 1:43am UTC](https://discourse.julialang.org/t/calling-a-function-with-2-arguments-of-type-float64-and-a-struct-via-its-dotted-version/49817/1 "2020-11-09T01:43:16Z")

</div>

Hi there,  
I have defined a struct and a function (at the Julia REPL) like:

> using Parameters
> 
> @with\_kw struct Cosmo\_model  
> Omega\_m0::Float64 = 0.3  
> Omega::FLoat64\_Lambda0 =0.7  
> end
> 
> function E\_Hubble(z::Float64, cosmo::Cosmo\_model)  
> @unpack Omega\_m0, Omega\_Lambda0 = cosmo  
> sqrt(Omega\_m0\*(1 + z)^3 + Omega\_Lambda0)  
> end

After defining:

> cosmo = Cosmo\_model()

when I call E\_Hubble with the first argument a “scalar”, such as:

> E\_Hubble(1.0, cosmo)

it works fine. However, I could not manage to make it work with an array, with its dotted version, such as:

> z = [0.0, 1.0]  
> E\_Hubble.(z, cosmo)

I get the following error:

ERROR: MethodError: no method matching length(::Supercosmo\_model)  
Closest candidates are:  
length(::Core.MethodTable) at reflection.jl:957  
length(::CompositeException) at task.jl:41  
length(::Base.Iterators.Flatten{Tuple{}}) at iterators.jl:1061

I then have 2 questions:

(1) how can I make it work with an array for the first argument?

(2) is the general way I have coded the struct and the function above the best one, under all aspects, including performance? Should I really use the Parameters package or should I stick to Julia core? Any specific comments are very much welcome, since this is one of my first codings in Julia…

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 9, 2020, 2:47am UTC](https://discourse.julialang.org/t/calling-a-function-with-2-arguments-of-type-float64-and-a-struct-via-its-dotted-version/49817/2 "2020-11-09T02:47:39Z")

</div>

> [@mocalvao](#):
>
> (1) how can I make it work with an array for the first argument?

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:

```julia
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:

```julia
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.

> [@mocalvao](#):
>
> (2) is the general way I have coded the struct and the function above the best one, under all aspects, including performance? Should I really use the Parameters package or should I stick to Julia core? Any specific comments are very much welcome, since this is one of my first codings in Julia…

Your code looks fine, but it will be easier to read if you can [quote your code](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) so that it is formatted properly.

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

```julia
@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.

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [November 9, 2020, 2:58am UTC](https://discourse.julialang.org/t/calling-a-function-with-2-arguments-of-type-float64-and-a-struct-via-its-dotted-version/49817/3 "2020-11-09T02:58:44Z")

</div>

Thanks for the prompt and informative reply.

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [November 9, 2020, 7:55pm UTC](https://discourse.julialang.org/t/calling-a-function-with-2-arguments-of-type-float64-and-a-struct-via-its-dotted-version/49817/4 "2020-11-09T19:55:50Z")

</div>

I would like to further plot and use @manipulate, like:

```julia
z = 0.0:0.01:3.0
Omegas = 0.1:0.01:1.0
mp = @manipulate for Omega_m0 in Omegas
  cosmo = Cosmo_model(Omega_m0, Omega_Lambda0)
  E_H = E_Hubble.(z, Ref(cosmo))
  plot(z, E_H)
end

```

However, no plot is generated at all ☹
