# Extending function from Base

**URL:** <https://discourse.julialang.org/t/extending-function-from-base/53414>\
**Category:** General Usage\
**Tags:** function, base\
**Created:** [January 15, 2021, 10:31pm UTC](https://discourse.julialang.org/t/extending-function-from-base/53414 "2021-01-15T22:31:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr.red](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mr.red](https://discourse.julialang.org/u/mr.red)\
**Post date:** [January 15, 2021, 10:31pm UTC](https://discourse.julialang.org/t/extending-function-from-base/53414/1 "2021-01-15T22:31:11Z")

</div>

The function `Base.binomial(n, k)` has 3 different calling patterns:

```julia
julia> methods(Base.binomial)
# 3 methods for generic function "binomial":
[1] binomial(n::BigInt, k::UInt64) in Base.GMP at gmp.jl:641
[2] binomial(n::BigInt, k::Integer) in Base.GMP at gmp.jl:642
[3] binomial(n::T, k::T) where T<:Integer in Base at intfuncs.jl:933

```

I implemented an extension to real/complex arguments in a module `SpecialFunctions`:

```julia
binomial(x::Number, y::Number) = inv((x+1) * beta(x-y+1, y+1))

```

My problem now is that after `using SpecialFunctions` in REPL I explictly need to say which `binomial` I want to call.

```julia
julia> using SpecialFunctions
julia> binomial(1,1)
WARNING: both SpecialFunctions and Base export "binomial"; uses of it in module Main must be qualified
ERROR: UndefVarError: binomial not defined
Stacktrace:
 [1] top-level scope at REPL[1]:1
 [2] run_repl(::REPL.AbstractREPL, ::Any) at /build/julia/src/julia-1.5.3/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:288
julia> Base.binomial(1,1)
1
julia> SpecialFunctions.binomial(1,1)
1.0

```

How should I define `SpecialFunctions.binomial` such that `Base.binomial` is still called by default if the arguments make it possible, e.g. Integer arguments?

---

<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, 2021, 10:43pm UTC](https://discourse.julialang.org/t/extending-function-from-base/53414/3 "2021-01-15T22:43:12Z")

</div>

Presumably this is in reference to [SpecialFunctions.jl#293](https://github.com/JuliaMath/SpecialFunctions.jl/pull/293). If you want to extend a Base method (defined only for integers) with a more general method for other number types, you can simply define:

```julia
Base.binomial(x::Number, y::Number) = inv((x+1) * beta(x-y+1, y+1))

```

(I feel like this is explained in the Julia manual, but I can’t find the section?)

(This is [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-type-piracy), but might be acceptable here?)

---

<div class="post-metadata">

**Author:** ![mr.red](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mr.red](https://discourse.julialang.org/u/mr.red)\
**Post date:** [January 15, 2021, 10:49pm UTC](https://discourse.julialang.org/t/extending-function-from-base/53414/4 "2021-01-15T22:49:02Z")

</div>

Thanks. Easy solution.

Regarding the type piracy: I don’t see any direct issues. Lets see if anybody voices concerns for the pull request…

---

<div class="post-metadata">

**Author:** ![mr.red](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mr.red](https://discourse.julialang.org/u/mr.red)\
**Post date:** [January 15, 2021, 10:55pm UTC](https://discourse.julialang.org/t/extending-function-from-base/53414/5 "2021-01-15T22:55:21Z")

</div>

Following your solution and extending `Base.binomial` do we still need to export binomial in the module.  
What I mean: In the file `SpecialFunctions.jl` do we need a line `export binomial`?

---

<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:** [January 15, 2021, 10:58pm UTC](https://discourse.julialang.org/t/extending-function-from-base/53414/6 "2021-01-15T22:58:58Z")

</div>

Nope, no need to export. Defining Base.binomial put’s it in base and available to everyone.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [January 16, 2021, 4:11pm UTC](https://discourse.julialang.org/t/extending-function-from-base/53414/8 "2021-01-16T16:11:56Z")

</div>

It is type piracy, but it is a more generic case than the others, consequently, it will only be called unintentionally in situations someone would trigger an error (a `MethodError` more specifically). Not ideal (can lead to subtle bugs from someone that is programming by try-and-error instead of using documentation) but not broken I think.
