# Precompile function for types defined in other modules

**URL:** <https://discourse.julialang.org/t/precompile-function-for-types-defined-in-other-modules/27410>\
**Category:** New to Julia\
**Created:** [August 11, 2019, 11:09am UTC](https://discourse.julialang.org/t/precompile-function-for-types-defined-in-other-modules/27410 "2019-08-11T11:09:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fverdugo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fverdugo/32/9446_2.png) [@fverdugo](https://discourse.julialang.org/u/fverdugo)\
**Post date:** [August 11, 2019, 11:09am UTC](https://discourse.julialang.org/t/precompile-function-for-types-defined-in-other-modules/27410/1 "2019-08-11T11:09:51Z")

</div>

Assume that I have a module `Foo` that defines a function `foo`, and another module `Bars` (note the final s) that **depends on** `Foo` and defines a new type `Bar`. Is it possible to precompile function `foo` for arguments of type `Bar` ??

Source code to clarify what I am talking about:

```julia
# File Foo.jl
module Foo
export foo

@generated function foo(x::T) where T
  println("Compiling foo function for type $T")
  quote
    x
  end
end

precompile(foo,(Int,))

end #module

```

```julia
# File Bars.jl
module Bars
using Foo
export Bar

struct Bar{T}
  x::T
end

precompile(foo,(Bar{Int},))

end # module

```

Checking what gets precompiled:

```julia
julia> push!(LOAD_PATH, pwd());

julia> using Foo
[Info: Recompiling stale cache file /home/verdugo/.julia/compiled/v1.1/Foo.ji for Foo [top-level]
Compiling foo function for type Int64

julia> using Bars
[Info: Recompiling stale cache file /home/verdugo/.julia/compiled/v1.1/Bars.ji for Bars [top-level]
Compiling foo function for type Bars.Bar{Int64}

julia> foo(1) # This statement is using the precompiled cache. As expected.
1

julia> bar = Bar(1)
Bar{Int64}(1)

julia> foo(bar) # This statement is not using the precompiled cache, why??
Compiling foo function for type Bar{Int64}
Bar{Int64}(1)

julia> foo(bar) # Now, it does not compile. As expected.
Bar{Int64}(1)

```

I think the problem is that `foo` function can only be precompiled by the `Foo` module. But, how to precompile it for type `Bar`? Is it possible?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [August 11, 2019, 6:47pm UTC](https://discourse.julialang.org/t/precompile-function-for-types-defined-in-other-modules/27410/2 "2019-08-11T18:47:11Z")

</div>

My guess is that when Bars does the precompile it’s only compiling the function **in** Bars, so that if the Bars module were to call foo() it would use the precompiled version. Maybe if you exported foo in the Bars module?

---

<div class="post-metadata">

**Author:** ![fverdugo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fverdugo/32/9446_2.png) [@fverdugo](https://discourse.julialang.org/u/fverdugo)\
**Post date:** [August 12, 2019, 6:36am UTC](https://discourse.julialang.org/t/precompile-function-for-types-defined-in-other-modules/27410/3 "2019-08-12T06:36:05Z")

</div>

I have tried to export foo in Bars, but, it does not work unfortunately

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 12, 2019, 6:44am UTC](https://discourse.julialang.org/t/precompile-function-for-types-defined-in-other-modules/27410/4 "2019-08-12T06:44:37Z")

</div>

Looks like it’s related to this issue?

[https://github.com/JuliaLang/julia/pull/31466](https://github.com/JuliaLang/julia/pull/31466)

---

<div class="post-metadata">

**Author:** ![fverdugo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fverdugo/32/9446_2.png) [@fverdugo](https://discourse.julialang.org/u/fverdugo)\
**Post date:** [August 12, 2019, 7:09am UTC](https://discourse.julialang.org/t/precompile-function-for-types-defined-in-other-modules/27410/5 "2019-08-12T07:09:21Z")

</div>

> [@tkf](#):
>
> Looks like it’s related to this issue?

Yes! I hope it is fixed soon
