Julia: `undef` does not have parseable `repr`

Created on 9 Sep 2019  Â·  10Comments  Â·  Source: JuliaLang/julia

julia> VERSION
v"1.2.0"

julia> repr(undef)
"array initializer with undefined values"

It seems needless, to me, not to have a "parseable" representation here (https://github.com/JuliaLang/julia/pull/30757) (in the sense x == eval(Meta.parse(repr(x)))).

We sort of workaround this in a few places, by having Base.undef_ref_str
https://github.com/JuliaLang/julia/blob/90d481e6ca1f50758841d980d0810967ccc627f6/base/show.jl#L1848

As far as I can tell, we could just change the current show method from
https://github.com/JuliaLang/julia/blob/90d481e6ca1f50758841d980d0810967ccc627f6/base/show.jl#L3
to only be for the text/plain MIME type,
e.g.

show(io::IO, ::MIME"text/plain", ::UndefInitializer) =  print(io, "array initializer with undefined 
values")

Since we have
https://github.com/JuliaLang/julia/blob/4111609c2b49372d5139c0eb9119b738bf4d73d5/base/boot.jl#L401-L402
perhaps if we wanted more compact printing we could also define

show(io::IO, ::UndefInitializer) = print(io, "undef")
display and printing

Most helpful comment

I would also be in favor of printing UndefInitializer() instead of undef, since it seems more informative to make it clear that it is an instance of UndefInitializer.

All 10 comments

Slightly separate from the issue of a parseable repr: we only sometimes get the more compact Base.undef_ref_str printing in cases we probably want it:
e.g.
```julia
julia> fill(undef)
0-dimensional Array{UndefInitializer,0}:
array initializer with undefined values

julia> Array{Any, 0}(undef)
0-dimensional Array{Any,0}:

undef

It seem to me that something like
```julia
julia> [undef undef undef undef]
1×4 Array{UndefInitializer,2}:
 array initializer with undefined values  …  array initializer with undefined values

would be much clearer as

julia> [undef undef undef undef]
1×4 Array{UndefInitializer,2}:
 undef  undef  undef  undef

would be much clearer as

julia> [undef undef undef undef] 1×4 Array{UndefInitializer,2}: undef undef undef undef

I disagree. undef is only to initalize arrays with undefined values. It is not an undef value itelf. It is not the same as #undef (which if not a first class Julia object). You should probably never store it inside anything.

undef is only to initalize arrays with undefined values. It is not an undef value itelf. It is not the same as #undef

That seems fair enough, and we do get a "good" display in sensible use cases, e.g.

julia> Matrix(undef, 1, 4)
1×4 Array{Any,2}:
 #undef  #undef  #undef  #undef

Anyway, this was an aside :)

The main issue is the second case here:

# this seems fine
julia> UndefInitializer()  
array initializer with undefined values

# expected `undef` or `UndefInitializer()`
julia> repr(UndefInitializer())
"array initializer with undefined values" 

What is the use case here, i.e. what are you trying to do where this is a problem?

I noticed this in https://github.com/JuliaLang/julia/pull/33206/, trying to fix #31481

my main use-case of repr in general is to answer "how do I create a value like this?"

c = fill(undef)

seems like you tried to fill an array with undefined values and mistakenly used the undef array initializer. To me, the current printing prevented a bug.

certainly open to suggestions on the 0-dim undef case - i.e. how to print Array{Any, 0}(undef) -- but I think PR #33206 may be a better place to discuss that case :)

Should be easy to fix: just change base/show.jl from show(io::IO, ::UndefInitializer) = print(io, "array initializer with undefined values") to:

show(io::IO, ::UndefInitializer) = print(io, "undef")
show(io::IO, ::MIME"text/plain", ::UndefInitializer) = print(io, "undef: array initializer with undefined values")

This would keep the verbose interactive display output, but would change the output in containers, print, repr(undef), etc.

I would be in favor of this change.

I would also be in favor of printing UndefInitializer() instead of undef, since it seems more informative to make it clear that it is an instance of UndefInitializer.

Was this page helpful?
0 / 5 - 0 ratings