# Function depending on the global variable inside module

**URL:** <https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322>\
**Category:** New to Julia\
**Created:** [July 9, 2021, 6:20am UTC](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322 "2021-07-09T06:20:47Z")\
**Posts on this page:** 1\
**Showing post:** 18

<div class="post-metadata">

**Author:** ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)\
**Post date:** [August 4, 2021, 5:56pm UTC](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/18 "2021-08-04T17:56:55Z")

</div>

> [@Shuhua](#):
>
> For instance, if a `struct` has 10 fields, then do we need to specify 10 type parameters for each field?

Yes.

First, for type stability. (cf. [Avoid fields with abstract type](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-fields-with-abstract-type))

Second, for composability with various packages that cannot be predicted in advance. (Example: Composability with MonteCarloMeasurements.jl)

If you really don’t want to write type parameters, you can use [ConcreteStructs.jl](https://github.com/jonniedie/ConcreteStructs.jl). The macro `ConcreteStructs.@concrete` automatically add type parameters to all fields in struct.

See [[ANN] ConcreteStructs.jl - Cut the boilerplate when concretely parameterizing structs](https://discourse.julialang.org/t/ann-concretestructs-jl-cut-the-boilerplate-when-concretely-parameterizing-structs/47274)

**Small working example.** (Note that we have an issue someone should solve. [Play nicely with other struct def macros · Issue #4 · jonniedie/ConcreteStructs.jl · GitHub](https://github.com/jonniedie/ConcreteStructs.jl/issues/4))

```julia
using ConcreteStructs

Base.@kwdef @concrete struct Problem g; y0; v0; tspan end
Problem(;
    g = 9.80665,
    y0 = 0.0,
    v0 = 30.0,
    tspan = (0.0, 8.0)
) = Problem(g, y0, v0, tspan)

prob1 = Problem()

```

Output: `Problem{Float64, Float64, Float64, Tuple{Float64, Float64}}(9.80665, 0.0, 30.0, (0.0, 8.0))`

```julia
prob2 = Problem(v0 = 100.0)

```

Output: `Problem{Float64, Float64, Float64, Tuple{Float64, Float64}}(9.80665, 0.0, 100.0, (0.0, 8.0))`

```julia
using MonteCarloMeasurements
prob3 = Problem(y0 = 0.0 ± 0.0, v0 = 30.0 ± 1.0)

```

Output: `Problem{Float64, Particles{Float64, 2000}, Particles{Float64, 2000}, Tuple{Float64, Float64}}(9.80665, 0.0, 30.0 ± 1.0, (0.0, 8.0))`

---

_[View the full topic](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322)._
