Which I would expect would print… nothing rather than the word “nothing”.
The string function, and perhaps others, seems to behave similarly e.g.:
julia> string(pi)
"π"
And obviously using spintf or similar functions, we can avoid these issues.
julia> using Printf
julia> @sprintf("%.15f",pi)
"3.141592653589793"
This is an issue for me when I want to print things in a function which has optional inputs, the optional input is nothing when not provided,… leading to Julia now printing the word “nothing”. The context is this GitHub issue for those who want to learn more about that I am trying to do. I have a workaround, it just seems annoying that a thing I tend to use as a number, e.g. pi doesn’t get converted to a number here.
The constant pi is indeed a number, but it has the type irrational if I am not mistaken. And an irrational number can not be losslessly converted to a string as required for print. Except for, or course, convertion to the name of the constant, which is the behaviour you are seeing. The reason multiplication does what you want is that it promotes the irrational factor to a floating point product.
So you want to promote two different types: nothing, and number (which is an abstract type). Would it make sense to convert any input to a common type before printing?
XML.jl does a form of serialization by expanding their XML type to an XML string representing the document, and probably should specialize that serialization of nothing to the empty string and Irrational to a numeric representation of the number, instead of relying on print directly.
As for why print has that behavior - it’s for human readable output/canonical representation of a value, not a machine readable specific format.
What gives me pause at the moment is that this behavior is not consistent with the REPL display. That is the REPL seems ro have a special case for nothing and does not display it.