# Prepended underscore in Base functions

**URL:** <https://discourse.julialang.org/t/prepended-underscore-in-base-functions/19645>\
**Category:** New to Julia\
**Created:** [January 15, 2019, 2:53am UTC](https://discourse.julialang.org/t/prepended-underscore-in-base-functions/19645 "2019-01-15T02:53:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aortizb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aortizb/32/6513_2.png) [@aortizb](https://discourse.julialang.org/u/aortizb)\
**Post date:** [January 15, 2019, 2:53am UTC](https://discourse.julialang.org/t/prepended-underscore-in-base-functions/19645/1 "2019-01-15T02:53:50Z")

</div>

I see that in Base there are some functions that are prepended by an underscore. For instance, in Array.jl:

```julia
_collect(cont, itr, ::HasEltype, isz::SizeUnknown)

```

Is this prepended underscore a convention in Julia to indicate that a function is local to a module/method/function and thus not intended to be exported?

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [January 15, 2019, 3:05am UTC](https://discourse.julialang.org/t/prepended-underscore-in-base-functions/19645/2 "2019-01-15T03:05:21Z")

</div>

The convention is for these functions to be called as part of a pipeline dispatch, for example,

```julia
collect(::Type{T}, itr) where {T} = _collect(T, itr, IteratorSize(itr))

_collect(::Type{T}, itr, isz::HasLength) where {T} = copyto!(Vector{T}(undef, Int(length(itr)::Integer)), itr)
_collect(::Type{T}, itr, isz::HasShape) where {T} = copyto!(similar(Array{T}, axes(itr)), itr)
function _collect(::Type{T}, itr, isz::SizeUnknown) where T
    a = Vector{T}()
    for x in itr
        push!(a,x)
    end
    return a
end

```

in that case, the non underscored prepended function is called first and then it calls a method which has the prepended underscored and same name.

The idea is to have a dispatch on only the necessary arguments, but use dispatch to get the desired behavior for each type/case. Because how these are used, the exported functions usually are limited to the non-underscored prepended functions as these are internal and only used in the dispatch pipeline.

---

<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:** [January 15, 2019, 4:49pm UTC](https://discourse.julialang.org/t/prepended-underscore-in-base-functions/19645/3 "2019-01-15T16:49:02Z")

</div>

> [@aortizb](#):
>
> Is this prepended underscore a convention in Julia to indicate that a function is local to a module/method/function and thus not intended to be exported?

Yes, by convention a single leading underscore refers to functions for internal use only, [similar to Python](https://www.python.org/dev/peps/pep-0008/).

> [@Nosferican](#):
>
> The convention is for these functions to be called as part of a pipeline dispatch, for example,

This is one example, but there are lots of other internal functions, e.g. `Base._unsetenv` that are not used in this way.

In general, an prepended underscore is typically used to indicate some kind of internal implementation detail, especially functions that make special assumptions about the arguments and so aren’t safe for external use.
