-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
print the type parameter of Nullable #14304
Conversation
Well, I think the reason why it's not printed is that it's quite verbose and obscures the value. |
I like it better with the type parameter personally. We show the element type parameter with arrays. |
Yes, but for arrays it makes sense to have an abstract type for element type. It also greatly affects the behavior of the array, which justifies printing it. For Finally, the ratio (type information)/contents is quite higher for a one-value object like |
@nalimilan I see your points; this may look a little bit verbose and if But I think we should stick to the principle of least astonishment in this case. The docsting of
|
"Least astonishment" doesn't help you here, as |
|
Just printing julia> Nullable{Int8}(1)
Nullable(1)
julia> Nullable{Int128}(1)
Nullable(1)
julia> Nullable{UInt8}(1)
Nullable(0x01)
julia> Nullable{UInt16}(1)
Nullable(0x0001) |
Yeah, but just like |
+1 to the printing the type, and I think it can be justified through consistency, as |
Non-trivial values will output their own type anyway, so this will print the type twice in many occasions: julia> Nullable([1 2; 3 4])
Nullable(2x2 Array{Int64,2}:
1 2
3 4) This would become
|
@eschnett True. Perhaps the solution would be only to print a In general this discussion seems to reflect tensions in the intended use of the |
@davidagold I think the distinction runs between immutable and mutable containers. If a container can be modified, then it's important to know its abstract element type. However, if a container is immutable and holds at most a single element, then it's less important to know the abstract element type -- a particular container object will only ever hold the value at hand, which has a concrete type. |
@eschnett That's also a good point. Do you think this constitutes a significant enough disanalogy between |
Another point is that I'd like to know if I'm getting eg a |
@davidagold Yes, I think so. No, I don't know about any other immutable containers. |
Perhaps just don't print leaf types, but do print abstract ones? |
Given @eschnett 's observation, is it reasonable to ask whether or not non-empty julia> x = Nullable{Integer}(1)
Nullable(1)
julia> isa(x, Nullable{Int})
false
julia> typeof(get(x))
Int64 i.e. that the type parameter for a non-empty Relatedly, what does the compiler see/do when it encounters a non-empty EDIT: I suppose one very good reason to have abstract type parameters for nonempty |
A concrete type parameter like in |
Printing the type parameter seems like a good idea to me given how verbose things already are. |
I think it is okay to be a little bit verbose for the If we regard a function show{T}(io::IO, x::Nullable{T})
print(io, "Nullable{", T, "}(")
if !isnull(x)
show(io, x.value)
end
print(io, ')')
end
function print(io::IO, x::Nullable)
if isnull(x)
print(io, "null")
else
print(io, get(x))
end
end |
That seems like a good distinction, @bicycle1885. 👍 |
It seems to me that
That said, IMO if it's a non-concrete type the full type should be shown. BUT that's a different issue to this one about Nullable. I wonder if, since Nullable Array is a frequent case, we should optimize the output / make it less verbose:
should be
or similar. |
Going with @johnmyleswhite comment, I don't think the added complexity of coming up with a heuristic or rule when the printing type parameter information is redundant or not is worth it. |
print the type parameter of Nullable
This fixes
show{T}(io::IO, x::Nullable{T})
to always show the type parameter of a Nullable object.Before:
After:
Though there may be a deep consideration in the current behavior, I think showing type parameters is a conventional way in Julia.