# 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:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [August 4, 2021, 11:08pm UTC](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/21 "2021-08-04T23:08:45Z")

</div>

> [@CRquantum](#):
>
> By saying multithreading, do you mean things like openMP?

no I mean things like

`@threads for i in 1:n ....` or `@spawn begin .... end`

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 5, 2021, 12:10am UTC](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/22 "2021-08-05T00:10:38Z")

</div>

Yes, that is basically “like OpenMP:” shared-memory threads, with decorator directives to make it easy. As opposed to distributed-memory programming like MPI.

---

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [August 5, 2021, 3:58am UTC](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/23 "2021-08-05T03:58:54Z")

</div>

> [@genkuroki](#):
>
> 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)

Thanks. The second point above seems to be a design choice. Sometimes I feel that it is quite cumbersome to specify the argument type in a function if that type itself is a parametric one.

Consider again your above example. In `solve(prob::Problem, alg::EulerMethod)`, no constraints are exerted on `Problem`. However, I usually have some constraints in mind during design. For example, I will not expect that the type parameter `G` to be a `String` (at least in my design). Then, shall I enforce this constraint in the signature of `solve(prob::Problem, alg::EulerMethod)` or simply throw an error during execution?

---

<div class="post-metadata">

**Author:** ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)\
**Post date:** [August 5, 2021, 4:32am UTC](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/24 "2021-08-05T04:32:08Z")

</div>

Thank you!  
Yes, I was thinking it must be something related with shared memory stuff that can cause issue for global variable. For MPI, it seems using global variable is safe because each core are independent.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 5, 2021, 11:21am UTC](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/25 "2021-08-05T11:21:51Z")

</div>

> [@Shuhua](#):
>
> Then, shall I enforce this constraint in the signature of `solve(prob::Problem, alg::EulerMethod)` or simply throw an error during execution?

I think both options can be fine. Sometimes I prefer to throw an error on execution, because the error message can be more user friendly.

---

<div class="post-metadata">

**Author:** ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)\
**Post date:** [August 5, 2021, 12:41pm UTC](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/26 "2021-08-05T12:41:35Z")

</div>

Thanks @lmiq .

> [@lmiq](#):
>
> Sometimes I prefer to throw an error on execution

Did you mean we check the type on the first line of the function body (e.g., using something like `@assert`) or just let it run and encounter finally an error spontaneously? 😄

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 5, 2021, 12:47pm UTC](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/27 "2021-08-05T12:47:21Z")

</div>

Yes, one or other option. The use of an assertion allow you to throw a customized error message.

(personally I would like to have an option to throw an error without launching the complete stack trace, meaning, I would like to have something that behaves like this:

```julia
julia> function f(x)
         if typeof(x) != Int
           println("x must be an Int")
           return nothing
         end
         x
       end
f (generic function with 1 method)

julia> f(1.0)
x must be an Int

```

Instead of:

```julia
julia> function f(x)
         @assert typeof(x) == Int "x must be an Int"
         x
       end
f (generic function with 1 method)

julia> f(1.0)
ERROR: AssertionError: x must be an Int
Stacktrace:
 [1] f(x::Float64)
   @ Main ./REPL[6]:2
 [2] top-level scope
   @ REPL[7]:1

```

In this case the stack trace is not large, but in some cases it can be overwhelming for a user.

[Previous page](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322.md?page=1)
