Julia: `vec[nothing]` fails to print its error message

Created on 7 Jan 2019  路  4Comments  路  Source: JuliaLang/julia

Expected:

julia> [1]["foo"]
ERROR: ArgumentError: invalid index: foo of type String
Stacktrace:
 [1] to_index(::String) at ./indices.jl:266
 [2] to_index(::Array{Int64,1}, ::String) at ./indices.jl:247
 [3] to_indices at ./indices.jl:294 [inlined]
 [4] to_indices at ./indices.jl:291 [inlined]
 [5] getindex(::Array{Int64,1}, ::String) at ./abstractarray.jl:905
 [6] top-level scope at none:0

Actual:

julia> [1][nothing]
ERROR: ArgumentError: `nothing` should not be printed; use `show`, `repr`, or custom output instead.
Stacktrace:
 [1] print(::Base.GenericIOBuffer{Array{UInt8,1}}, ::Nothing) at ./show.jl:566
 [2] print_to_string(::String, ::Vararg{Any,N} where N) at ./strings/io.jl:122
 [3] string at ./strings/io.jl:155 [inlined]
 [4] to_index(::Nothing) at ./indices.jl:266
 [5] to_index(::Array{Int64,1}, ::Nothing) at ./indices.jl:247
 [6] to_indices at ./indices.jl:294 [inlined]
 [7] to_indices at ./indices.jl:291 [inlined]
 [8] getindex(::Array{Int64,1}, ::Nothing) at ./abstractarray.jl:905
 [9] top-level scope at none:0

julia> versioninfo()
Julia Version 1.0.3
Commit 099e826241 (2018-12-18 01:34 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E3-1505M v5 @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, skylake)
error handling good first issue

Most helpful comment

Amusing. I think we should revert that change and only make interpolating nothing into Cmd objects an error since that was the original motivating case.

All 4 comments

Amusing. I think we should revert that change and only make interpolating nothing into Cmd objects an error since that was the original motivating case.

Every time this comes up and it is fixed, it seems the end result is an improvement over the previous situation though. For example, using show correctly here would give invalid index: "foo" of type String in the case that didn't error, which is nicer.

Agreed; this should be using show/repr for the value anyway.

In particular, this line should use $(repr(i, context=:limit=>true)) instead of $i.

Similarly, this line should use $(repr(I, context=:limit=>true)) instead of $I.

(The missing :limit context was also a problem since otherwise huge arrays could get dumped into the error message.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sbromberger picture sbromberger  路  3Comments

ararslan picture ararslan  路  3Comments

i-apellaniz picture i-apellaniz  路  3Comments

arshpreetsingh picture arshpreetsingh  路  3Comments

Keno picture Keno  路  3Comments