# Using of non exported functions of Base in package code

**URL:** <https://discourse.julialang.org/t/using-of-non-exported-functions-of-base-in-package-code/71225>\
**Category:** General Usage\
**Created:** [November 9, 2021, 10:03pm UTC](https://discourse.julialang.org/t/using-of-non-exported-functions-of-base-in-package-code/71225 "2021-11-09T22:03:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![f\_u](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f_u/32/30639_2.png) [@f\_u](https://discourse.julialang.org/u/f_u)\
**Post date:** [November 9, 2021, 10:03pm UTC](https://discourse.julialang.org/t/using-of-non-exported-functions-of-base-in-package-code/71225/1 "2021-11-09T22:03:51Z")

</div>

During the development of a package I needed some reflective informations of a Union datatype. I used the following formulation:

```julia
Optional{T} = Union{Nothing, T}

struct Address
   street::String
   town::String
end

struct TestStruct
   address::Optional{Address}
end

```

in one of the package functions I need to know which Type besides Nothing is inside the Union type. For this I used Base.uniontypes. My question is now: Is there any other way to get the datatypes besides what I used and is it appropriate to use non exported functions if they fits very well?  
Opinions and suggestions are highly appreciated.

---

<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:** [November 10, 2021, 1:32am UTC](https://discourse.julialang.org/t/using-of-non-exported-functions-of-base-in-package-code/71225/2 "2021-11-10T01:32:38Z")

</div>

> [@f\_u](#):
>
> in one of the package functions I need to know which Type besides Nothing is inside the Union type.

Why doesn’t `f(::Optional{T}) where {T} = T` work?

---

<div class="post-metadata">

**Author:** ![f\_u](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f_u/32/30639_2.png) [@f\_u](https://discourse.julialang.org/u/f_u)\
**Post date:** [November 10, 2021, 11:08am UTC](https://discourse.julialang.org/t/using-of-non-exported-functions-of-base-in-package-code/71225/3 "2021-11-10T11:08:33Z")

</div>

Well, I’ve tried your suggestion but I get only the Union type back. What I’m Interested in is the type T itself. I you use your suggestion you will get the following:  
f(::Optional{T}) where {T} = T with Optional{String} gives Union and I’m interested in String.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 10, 2021, 12:32pm UTC](https://discourse.julialang.org/t/using-of-non-exported-functions-of-base-in-package-code/71225/4 "2021-11-10T12:32:57Z")

</div>

The idea is to call this `f` with a value of type `Optional{T}`, for example `f("xxx")` (because `"xxx"` is a `String` which is an `Optional{T}` with `T=String`.

---

<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:** [November 10, 2021, 12:47pm UTC](https://discourse.julialang.org/t/using-of-non-exported-functions-of-base-in-package-code/71225/5 "2021-11-10T12:47:38Z")

</div>

If what you want is to pass the _type_ `Optional{T}` and get back `T`, then define:

```julia
f(::Type{Optional{T}}) where {T} = T

```

which gives:

```julia
julia> f(Optional{Int})
Int64

julia> f(Optional{String})
String

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 10, 2021, 2:23pm UTC](https://discourse.julialang.org/t/using-of-non-exported-functions-of-base-in-package-code/71225/6 "2021-11-10T14:23:19Z")

</div>

> [@stevengj](#):
>
> If what you want is to pass the _type_ `Optional{T}` and get back `T` , then define:
> 
> …

Note that you then have:

```julia
julia> f(Nothing)
ERROR: UndefVarError: T not defined

```

so you might want to decide on how to handle the `nothing` case in there too. For example:

```julia
julia> f(::Type{Optional{T}}) where {T} = (@isdefined T) ? T : nothing
f (generic function with 1 method)

julia> f(Optional{Int})
Int64

julia> f(Optional{String})
String

julia> f(Nothing)
nothing

```

---

<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:** [November 10, 2021, 2:33pm UTC](https://discourse.julialang.org/t/using-of-non-exported-functions-of-base-in-package-code/71225/7 "2021-11-10T14:33:00Z")

</div>

> [@kristoffer.carlsson](#):
>
> Note that you then have:
> 
> ```julia
> julia> f(Nothing)
> 
> ```

It seems a lot easier to handle this by just defining

```julia
f(::Type{Optional{T}}) where {T} = T
f(::Type{Nothing}) = nothing

```

rather than using `@isdefined`.

---

<div class="post-metadata">

**Author:** ![f\_u](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/f_u/32/30639_2.png) [@f\_u](https://discourse.julialang.org/u/f_u)\
**Post date:** [November 10, 2021, 2:37pm UTC](https://discourse.julialang.org/t/using-of-non-exported-functions-of-base-in-package-code/71225/8 "2021-11-10T14:37:06Z")

</div>

Thanks for your explanations. Many learned. 👍 😃
