Unitful.jl: typeof() vs manually given types

julia> 1.0s isa Quantity{Float64, 𝐓 , Unitful.FreeUnits{(Unitful.Unit{:Second, 𝐓}(0, 1//1),), 𝐓 , nothing}}
true

The culprit here is are overloads of show in Unitful

function show(io::IO, x::Unit{N,D}) where {N,D}
    show(io, FreeUnits{(x,), D, nothing}())
end

in conjunction with

function show(io::IO, x::Unitlike)
    showoperators = get(io, :showoperators, false)
    first = ""
    sep = showoperators ? "*" : " "
    foreach(sortexp(typeof(x).parameters[1])) do y
        print(io,first)
        showrep(io,y)
        first = sep
    end
    nothing
end

which will make Unitful.Unit{:Second, 𝐓}(0, 1//1) print as s.

Question is, why are you comparing types this way? I’d imagine it’d be more robust to check unit and dimension. But then again, isn’t a major point of working with units needing not to care about units?

3 Likes