`string(dict)` loses information

string(dict) loses information if dict contains Float64. How to avoid this behavior?

# on v1.0
x = 1.000000101e8 # Float64
y = 2.000000202e8 # Float64

d = Dict("x"=>x, "y"=>y)
d2 = eval(Meta.parse(string(d)))

d["x"] == d2["x"] # false

Dont assume that you can get back your data from an object by parsing the output of string on it.

1 Like

There seems to be context option in sprint.

x = 1.000000101e8
y = 2.000000202e8

d = Dict("x"=>x, "y"=>y)
d2 = eval(Meta.parse(string(d)))
d3 = eval(Meta.parse(sprint(show, d, context=:compact => false)))

d["x"] == d2["x"] # false
d["x"] == d3["x"] # true

Dictionary printing probably shouldn’t pass :compact by default. We don’t do that in vectors or single-column matrices anymore and it elf masker sense for Dict printing to match. But yes, in general, you can’t assume that object printing preserves all information.

Indeed one shouldn’t rely on that for string. which is just “give me a string at represents this in some way”.

I feel like we should be able to rely on it for repr.
I think we should have have python’s convention,
which is roughly: if it parses, then evaling it should give back the same as a deep copy.

But we did not adopt that convention, and it is a breaking change.
So you can not

A pointed out above, printing is not guaranteed to preserve information. If you are trying to save data, consider

or serialization.