# Function Definition only if type is defined

**URL:** https://discourse.julialang.org/t/function-definition-only-if-type-is-defined/51234
**Category:** New to Julia
**Created:** [December 4, 2020, 2:50am UTC](https://discourse.julialang.org/t/function-definition-only-if-type-is-defined/51234 "2020-12-04T02:50:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)
#### Post date: [December 4, 2020, 2:50am UTC](https://discourse.julialang.org/t/function-definition-only-if-type-is-defined/51234/1 "2020-12-04T02:50:15Z")

</div>

I have a file of utilities functions I use a lot. I just include the file whenever I need those functions. In particular, I have functions defined for several different types. One of the types is defined in a separate package and not base julia. But I only sometimes want to load the package. I would like to keep the function definition in the same place since I have several versions of this function depending on the type. But it throws an error if the package is not loaded because the type isn’t defined. How do I only defined the function if the type is defined? How do people typically organize functions in cases like this?

---

<div class="post-metadata">

### Author: ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)
#### Post date: [December 4, 2020, 3:40am UTC](https://discourse.julialang.org/t/function-definition-only-if-type-is-defined/51234/2 "2020-12-04T03:40:57Z")

</div>

You can you use `@isdefined Type` to check if the type is available in the current scope.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 4, 2020, 3:43am UTC](https://discourse.julialang.org/t/function-definition-only-if-type-is-defined/51234/3 "2020-12-04T03:43:08Z")

</div>

Your might also find [https://github.com/JuliaPackaging/Requires.jl](https://github.com/JuliaPackaging/Requires.jl) useful in order to only define the relevant functions if the package is loaded.
