Unitful and Plots, vector return types

The following behaviour surprised me: I would like to plot with data with units (Using Unitful), but it seems return type declarations causes something to break.

Silly example:

using Unitful, UnitfulRecipes, Plots
function calctempv(v)::Vector{Unitful.Temperature}
	[u"270.0K" + p*u"1K/bar" for p in v]
end
pressures = collect(u"1.0bar":u"1bar":u"30bar")
plot(pressures, calctempv(pressures))

error message: DimensionError: Inf and 271.0 K are not dimensionally compatible.

julia> typeof(calctempv(pressures))

Vector{Union{Quantity{T, 𝚯, U}, Level{L, S, Quantity{T, 𝚯, U}} where {L, S}} where {T, U}} (alias for Array{Union{Quantity{T, 𝚯, U}, Level{L, S, Quantity{T, 𝚯, U}} where {L, S}} where {T, U}, 1})

If I remove Vector{Unitful.Temperature}, everything works.

Complete example in Pluto notebook (gist)

Oh, :: vs. <: always bites me:

[1, 2]u"K" isa Vector{<:Unitful.Temperature} # true
[1, 2]u"K" isa Vector{Unitful.Temperature} # false

So I think you want:

function calctempv(v)::Vector{<:Unitful.Temperature}
	[u"270.0K" + p*u"1K/bar" for p in v]
end

or better yet, just don’t specify the return type

2 Likes

Leaving this here for future me once I forget about this again

1 Like

Sufficiently related, though: Why does not

plot(u"1K":u"1K":u"10K")

work?
(DimensionError: Inf and 1.0 K are not dimensionally compatible.)

Is this while using UnitfulRecipes? That seems to work as intended for me