Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undef does not have parseable repr #33204

Closed
nickrobinson251 opened this issue Sep 9, 2019 · 10 comments · Fixed by #33211
Closed

undef does not have parseable repr #33204

nickrobinson251 opened this issue Sep 9, 2019 · 10 comments · Fixed by #33211
Labels
display and printing Aesthetics and correctness of printed representations of objects.

Comments

@nickrobinson251
Copy link
Contributor

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

const undef_ref_str = "#undef"

As far as I can tell, we could just change the current show method from

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

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

julia/base/boot.jl

Lines 401 to 402 in 4111609

struct UndefInitializer end
const undef = UndefInitializer()

perhaps if we wanted more compact printing we could also define

show(io::IO, ::UndefInitializer) = print(io, "undef")
@nickrobinson251 nickrobinson251 changed the title undef/UndefInitializer() does not have parsable repr undef does not have parseable repr Sep 9, 2019
@nickrobinson251
Copy link
Contributor Author

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

@KristofferC
Copy link
Member

KristofferC commented Sep 10, 2019

would be much clearer as

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.

@nickrobinson251
Copy link
Contributor Author

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" 

@KristofferC
Copy link
Member

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

@nickrobinson251
Copy link
Contributor Author

I noticed this in #33206, trying to fix #31481

@nickrobinson251
Copy link
Contributor Author

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

@KristofferC
Copy link
Member

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.

@nickrobinson251
Copy link
Contributor Author

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

@stevengj
Copy link
Member

stevengj commented Sep 10, 2019

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.

@stevengj stevengj added the display and printing Aesthetics and correctness of printed representations of objects. label Sep 10, 2019
@stevengj
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants