# Getfield from package / module

**URL:** <https://discourse.julialang.org/t/getfield-from-package-module/57432>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 18, 2021, 2:32am UTC](https://discourse.julialang.org/t/getfield-from-package-module/57432 "2021-03-18T02:32:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephenll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephenll/32/28751_2.png) [@stephenll](https://discourse.julialang.org/u/stephenll)\
**Post date:** [March 18, 2021, 2:32am UTC](https://discourse.julialang.org/t/getfield-from-package-module/57432/1 "2021-03-18T02:32:09Z")

</div>

In a package I created, I am trying to call a function with a string, and using `getfield`, return a function from another package. The other package has been properly added to my package, and I could use the functions if called like `a = Poisson(5)`

The code below works, if I bring in the entire package with `using`. If I uncomment out the part of the `using` so only `Poisson`, `Gamma`, and `Normal` are allowed, then calling `MYcompare.foo("Poisson")` returns an error that _Distributions is not defined_.

I am trying to understand while restricting to those distributions won’t work but bringing in everything does.

```julia
module MYcompare
    using Distributions #: Poisson, Gamma, Normal
    export foo

    function foo(str)
        s = Symbol(str)
        f = getfield(Distributions,s)
        return f(5)
    end

end

```

---

<div class="post-metadata">

**Author:** ![stephenll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephenll/32/28751_2.png) [@stephenll](https://discourse.julialang.org/u/stephenll)\
**Post date:** [March 18, 2021, 4:09am UTC](https://discourse.julialang.org/t/getfield-from-package-module/57432/2 "2021-03-18T04:09:31Z")

</div>

Here is what I think is the reason that `Distributions` is not defined:

I re-read the manual on the `using` statement. Specifying which names to be brought in doesn’t bring in the name of the module. Changing the code above to:

```nohighlight
using Distributions: Distributions, Poisson, Gamma, Normal

```

works!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 18, 2021, 5:59am UTC](https://discourse.julialang.org/t/getfield-from-package-module/57432/3 "2021-03-18T05:59:29Z")

</div>

Yes, that’s the reason.

That said, note that this usage is not idiomatic and will generate suboptimal code.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 18, 2021, 6:22am UTC](https://discourse.julialang.org/t/getfield-from-package-module/57432/4 "2021-03-18T06:22:12Z")

</div>

Why is it suboptimal? It looks legit.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 18, 2021, 7:37am UTC](https://discourse.julialang.org/t/getfield-from-package-module/57432/5 "2021-03-18T07:37:52Z")

</div>

Types won’t be inferred.

---

<div class="post-metadata">

**Author:** ![stephenll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephenll/32/28751_2.png) [@stephenll](https://discourse.julialang.org/u/stephenll)\
**Post date:** [March 18, 2021, 3:41pm UTC](https://discourse.julialang.org/t/getfield-from-package-module/57432/6 "2021-03-18T15:41:54Z")

</div>

Thank you for the responses. I am using this to create a way to work with a large number of tests to compare the results between the Julia code and our in house c# so performance is not crucial. The Julia code that is eventually called follows idiomatic Julia and I think is type stable.

I’ve been using matlab for 20 years and then forced to use R for the last 5. I am in complete awe that I was able as a new user with Julia:

- Take a TOML file generated from another system with inputs and outputs
- Convert the TOML to a dictionary.
- Map some of those inputs to Julia Functions (the above code)
- Then convert the dictionary to a named tuple
- which is then splatted as the optional arguments to a struct constructor
- run the models/simulations with this composite type
- compare the results of the Julia simulations to those in the TOML file

All done in a ridiculously clear and small amount of code.
