Shadow Base name in REPL

Inspired by How about to simplify the factorial function name? - #14 by jar1.

Say I want to define !(x) = factorial(x). I can do that in a module

julia> baremodule M
       import Base
       !(x) = Base.factorial(x)
       Base.@show !5
       end
!5 = 120

but I can’t do it in the repl:

julia> !(x) = factorial(x)
ERROR: error in method definition: function Base.! must be explicitly imported to be extended

I don’t want to extend Base.!, so the advice it gives is not relevant. Is it possible to define ! in the repl? Can I have a bare repl?

That works just fine as long as you have not used ! in that session before:

julia> !(x) = factorial(x)
! (generic function with 1 method)

julia> !(5)
120

The important thing is that bindings from Base (or any other module) are not resolved when you try to shadow them:

$ julia -q
julia> sin = 2
2

$ julia -q
julia> sin(2)
0.9092974268256817

julia> sin = 2
ERROR: cannot assign a value to variable Base.sin from module Main
4 Likes