# Method definitions after const aliases

**URL:** <https://discourse.julialang.org/t/method-definitions-after-const-aliases/6291>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [October 6, 2017, 1:05pm UTC](https://discourse.julialang.org/t/method-definitions-after-const-aliases/6291 "2017-10-06T13:05:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 6, 2017, 1:05pm UTC](https://discourse.julialang.org/t/method-definitions-after-const-aliases/6291/1 "2017-10-06T13:05:37Z")

</div>

I am asking here before opening an issue, about the following:

I use `const some_alias = some_function` to define convenient shorthands. Especially for infix in tests, eg

```julia
const ≅ = isequal

```

If I later accidentally define methods for the alias, it seems to overwrite the original method:

```julia
julia> const ≅ = isequal # I define this as a nice shorthand
isequal (generic function with 22 methods)

julia> 1 ≅ 2
false

julia> ≅(a, b) = a+b # later I change my mind
WARNING: Method definition isequal(Any, Any) in module Base at operators.jl:87 overwritten in module Main at REPL[3]:1.                                                                          
isequal (generic function with 22 methods)

julia> isequal(1, 1)
ERROR (in the keymap): MethodError: no method matching +(::Base.LineEdit.Prompt, ::Base.LineEdit.
Prompt)                                                                                         
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:424
Stacktrace:
 [1] mode_idx(::Base.REPL.REPLHistoryProvider, ::Base.LineEdit.Prompt) at ./REPL.jl:390
 [2] add_history(::Base.REPL.REPLHistoryProvider, ::Base.LineEdit.PromptState) at ./REPL.jl:398
 [3] add_history(::Base.LineEdit.PromptState) at ./LineEdit.jl:596
 [4] commit_line(::Base.LineEdit.MIState) at ./LineEdit.jl:1306
 [5] (::Base.LineEdit.##92#122)(::Base.LineEdit.MIState, ::Base.REPL.LineEditREPL, ::Vararg{Any,N
} where N) at ./LineEdit.jl:1357                                                                
 [6] (::Base.LineEdit.##13#14{Base.LineEdit.##92#122,String})(::Base.LineEdit.MIState, ::Base.REP
L.LineEditREPL) at ./LineEdit.jl:740                                                            
 [7] prompt!(::Base.Terminals.TTYTerminal, ::Base.LineEdit.ModalInterface, ::Base.LineEdit.MIStat
e) at ./LineEdit.jl:1618                                                                        
 [8] run_interface(::Base.Terminals.TTYTerminal, ::Base.LineEdit.ModalInterface) at ./LineEdit.jl
:1578                                                                                           
 [9] run_frontend(::Base.REPL.LineEditREPL, ::Base.REPL.REPLBackendRef) at ./REPL.jl:945
 [10] run_repl(::Base.REPL.LineEditREPL, ::Base.##507#508) at ./REPL.jl:180
 [11] _start() at ./client.jl:413
julia> 

```

I recognize that I am violating a promise when I use `const` and change my mind later. Nevertheless, would it make sense to catch that instead when I define a method for `≅`?

The above is on `0.6`, `master` gives a slightly different error.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [October 6, 2017, 1:11pm UTC](https://discourse.julialang.org/t/method-definitions-after-const-aliases/6291/2 "2017-10-06T13:11:44Z")

</div>

I think this confuses the REPL because `isequal` does not work anymore (and thus the strange error with `LineEdit`). If you try the same with something less disrupting it works as expected:

```julia
julia> const ssin = sin                                                                                                                               
sin (generic function with 10 methods)                                                                                                                

julia> ssin(a::Int) = 5                                                                                                                               
sin (generic function with 11 methods)                                                                                                                
                                                                                                                                                      
julia> sin(5)                                                                                                                                         
5                                                                                                                                                     
                                                                                                                                                      
julia> ssin(5)                                                                                                                                        
5                                                                                                                                                     

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 6, 2017, 1:59pm UTC](https://discourse.julialang.org/t/method-definitions-after-const-aliases/6291/3 "2017-10-06T13:59:18Z")

</div>

> [@Tamas\_Papp](#):
>
> I recognize that I am violating a promise when I use const and change my mind later. Nevertheless, would it make sense to catch that instead when I define a method for ≅?

No you are not violating the `const`ness, you are just adding method to a function, i.e. mutating the object.
