Make Base Types const?


I’ve spent quite a bit of time hunting down a bug that was caused by redefining a Base type. E.g.:

Int = find(something)   # I am finding interior nodes in a grid

# a function defined somewhere else in the notebook
function do_something()
   A = Int[]
   push!(A, 5)

# ERROR: MethodError: no method matching push!(::Int64, ::Int64)

It took me a while to find this. Obviously I should have chosen better naming. But in a notebook neither syntax highlighting warned me, nor an actual Julia warning.

What surprised me is

  • const T = Int64; T = 5 throws an error
  • Int = 3 or Float64 = sqrt is ok.

Should these "standard types" for lack of a better name be `const`?


They are const, just in another module:

julia> eval(Core, :(Int = 5))
ERROR: invalid redefinition of constant Int
 [1] eval(::Module, ::Any) at ./boot.jl:235

If you use Int before you assign to it, at least you get a warning:

julia> Int

julia> Int = 5
WARNING: imported binding for Int overwritten in module Main

If you could not assign to a type (or something else) exported from Base, every new export from Base would potentially be breaking, so that’s not really an option.


Lint.jl picks this up:

julia> using Lint                                                                                                                     
julia> lintstr("Int = 4")                                                                                                             
1-element Array{Lint.LintMessage,1}:                                                                                                  
 none:1 I392 Int: local variable might cause confusion with a synonymous export from Base                                             

function fun()
    a = Int[]
    push!(a, 5)
Int = 1

First time executes ok, second time it throws an error. So technically I did use Int before redefining it. Or am I misunderstanding?

Anyhow, if this is not an option, then maybe I’ll just open an Issue in IJulia to improve syntax highlighting.


Is there a nice way to use Lint with notebooks?


Might be some way of integrating Lint with NBInclude.