Unitful.jl: type matching dimensions

Hi,

Up until now, I have only used Unitful for basic conversion, however I would like now to use it for type checking. However, I cannot make it work.

I want ensure (with type safety) that a given variable is an array of a specific dimension (energy here).

Here is the code:

energies = zeros(3, 3)u"eV"

const EnergyQuantity = Quantity{T, dimension(u"eV"), U} where T where U

# prints true, as expected
@show 3u"eV" isa EnergyQuantity
@show 3u"J" isa EnergyQuantity

# prints true, as expected as well
@show energies isa Array{T, 2} where T

# prints false !
@show energies isa Array{EnergyQuantity, 2}

I do not understand why the last line does not work. Is it due to limitations of the type system, or am I just not understanding how this work ?

Thanks !

This is not specific to Unitful: Types · The Julia Language

This last point is very important: even though Float64 <: Real we DO NOT have Point{Float64} <: Point{Real}.

!(energies isa Array{  EnergyQuantity, 2})
# but
  energies isa Array{<:EnergyQuantity, 2}
2 Likes

Thanks ! I had the feeling I did not really understand this section of the documentation.

Now I understand more, it is because EnergyQuantity is not the exact base type of energies, so we need to use the subtype operator <: to resolve it.

Thanks again !