# Can you expand your functionality based on other package availability?

**URL:** <https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [April 11, 2017, 6:04pm UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175 "2017-04-11T18:04:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![richardreeve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/richardreeve/32/2257_2.png) [@richardreeve](https://discourse.julialang.org/u/richardreeve)\
**Post date:** [April 11, 2017, 6:04pm UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175/1 "2017-04-11T18:04:19Z")

</div>

I want to expand a package I’ve written to provide extra functionality if another package is installed, but not to make that package compulsory. I don’t know whether it’s possible to do that, but it seems like it would be a useful thing… ideally I’d also like to put requirements on the version of the package.

I’ve tried defining functions if `Pkg.installed("Package")` is not nothing and/or if the version is okay, but it precompiles it once and doesn’t update if “Package” is subsequently installed:

```julia
module Expansion
if Pkg.installed("TestPkg") != nothing
    
    include("expandTestPkg.jl") # containing my expansion code
    export TestPkgExpansion
end
end

```

If I put `Pkg.installed("Package")` inside the function or type definition then I can’t make the function dispatch on a type only defined in the “Package”, or the type contain an element of that type.

Is there some way of doing this?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 11, 2017, 6:12pm UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175/2 "2017-04-11T18:12:23Z")

</div>

This is conditional dependency stuff. The “true solution” is in development:

[https://github.com/JuliaLang/julia/issues/15705](https://github.com/JuliaLang/julia/issues/15705)  
[https://github.com/JuliaLang/Juleps/issues/2](https://github.com/JuliaLang/Juleps/issues/2)

Currently, you should avoid doing something like this if you precompile your package because it can have some bad edge cases. However, some packages do stuff like this (Plots.jl as the notable example) and do mostly okay.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [April 11, 2017, 6:16pm UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175/3 "2017-04-11T18:16:07Z")

</div>

You could also take a look at [https://github.com/MikeInnes/Requires.jl](https://github.com/MikeInnes/Requires.jl).

But be warned: that package does some really non-standard stuff. It is also not very well maintained, i.e. bugs often stay unfixed for quite a while etc. I’m using it a lot, but I often wish I didn’t have to 🙂

---

<div class="post-metadata">

**Author:** ![richardreeve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/richardreeve/32/2257_2.png) [@richardreeve](https://discourse.julialang.org/u/richardreeve)\
**Post date:** [April 11, 2017, 6:39pm UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175/4 "2017-04-11T18:39:01Z")

</div>

Thanks @ChrisRackauckas. I did look at Plots.jl for inspiration, but to be honest I immediately got it to crash my Julia session, which I figured was not a good sign! I also can’t really follow how it’s doing it. However, Requires sounds like what I need - thanks @davidanthoff - it’s a shame if it’s so flaky…

I suspect if this is a feature that really is coming though, then I could make the packages compulsory for now and then optional when the feature appears. I see there’s another issue about it though:

[https://github.com/JuliaLang/julia/issues/6195](https://github.com/JuliaLang/julia/issues/6195)

which has been bumped back from 0.4 to 0.6 and now to 1.0… is it really going to be done?

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [April 11, 2017, 6:47pm UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175/5 "2017-04-11T18:47:07Z")

</div>

> [@richardreeve](#):
>
> which has been bumped back from 0.4 to 0.6 and now to 1.0… is it really going to be done?

I would take this 1.0 milestone on github with a grain of salt. The current official message is still that julia 1.0 is supposed to happen at juliacon (or at least an alpha build, which I assume means feature complete). There is absolutely no way on earth that all the things that currently have the 1.0 milestone on github will be done by then, so something will have to give (either the timeline or the scope for 1.0).

---

<div class="post-metadata">

**Author:** ![richardreeve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/richardreeve/32/2257_2.png) [@richardreeve](https://discourse.julialang.org/u/richardreeve)\
**Post date:** [April 11, 2017, 7:12pm UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175/6 "2017-04-11T19:12:30Z")

</div>

Hah, yes - I wasn’t exactly expecting the fact that it said 1.0 to mean it would be done by June! My question was more - are there lots of things like this that will probably get pushed back to 2.0, 3.0, etc.? I guess the answer to that is yes by your answer…

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [April 11, 2017, 7:15pm UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175/7 "2017-04-11T19:15:24Z")

</div>

Well, or they change the timeline… I have no insight into the thinking of the core dev group on this topic.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [April 12, 2017, 6:02am UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175/8 "2017-04-12T06:02:45Z")

</div>

Plots shouldn’t crash your Julia session, you should open an issue if it does.

---

<div class="post-metadata">

**Author:** ![richardreeve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/richardreeve/32/2257_2.png) [@richardreeve](https://discourse.julialang.org/u/richardreeve)\
**Post date:** [April 14, 2017, 10:42pm UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175/9 "2017-04-14T22:42:22Z")

</div>

@mkborregaard I’m sure you’re right. I couldn’t get it to compile under 0.6, and then I tried to play around with it under 0.5 and my session bombed out. I can’t reproduce it because it wasn’t what I was trying to do - I was just trying to get to grips with how it was handling multiple potential backends, but since I failed to understand the code too I just moved on I’m afraid.

---

<div class="post-metadata">

**Author:** ![richardreeve](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/richardreeve/32/2257_2.png) [@richardreeve](https://discourse.julialang.org/u/richardreeve)\
**Post date:** [April 14, 2017, 10:46pm UTC](https://discourse.julialang.org/t/can-you-expand-your-functionality-based-on-other-package-availability/3175/10 "2017-04-14T22:46:24Z")

</div>

For those who are interested, I was reassured to hear when I just asked in the last thread

> <https://github.com/JuliaLang/julia/issues/6195>
>
> currently, some modules (incorrectly and unfortunately) use \`Pkg.installed\` to d…ecide whether it should include some additional functionality. I would like to propose the following extension to module initialization that could help simplify this situation of optional dependencies:
> 
> \---
> \### Rules:
> 1. During module parse time, a module can declare a dependency on another module, given by name
> 2. Following the closing of a module (without error), and after all \`\_\_init\_\_\` functions have run, any modules that declared \`module B require Main.C\` will be (re-)loaded.
> \### Example:
> 
> \`\`\` julia
> module A
> module B requires Main.C
> # code in here is part of A iff Main.C is defined as a module
> # it's execution time is defined as after the call to A.\_\_init\_\_ and C.\_\_init\_\_
> using Main.C
> import ..A.something
> something(::C.CType) = 2
> end
> # code in here is always part of A
> something(::Any) = 1
> end
> \# Accessing A.B here would throw an UndefRef-like error, with a message that B requires C
> module C
> type CType end
> end
> \# Now A.B exists, and A.something() has extra functionality
> \`\`\`

from Stefan Karpinski:

> Yes, we really need this in 1.0 – it’s a big problem for the package ecosystem currently.
