Method definitions after const aliases

question

#1

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

const ≅ = isequal

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

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.


#2

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> 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                                                                                                                                                     

#3

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