Thanks very, very much for all your suggestions. My questions are resolved. Let me summarize:
The Julia idiomatic way is
using Unitful
Quantity(a,b) = Quantity{a, dimension(b), typeof(b)}
mutable struct Data4{Float <: AbstractFloat}
length::Quantity(Float,u"m")
end
d4 = Data4{Float64}(2.0u"mm")
@show d4.length
Nicer to read, but hard to understand the implementation behind it with the (bad) assumption that a type Float is defined outside of the macro (so the macro only works under this assumption):
using Unitful
macro Float_str(str)
return :( Quantity{$(esc(:Float)), dimension(@u_str($str)), typeof(@u_str($str))} )
end
mutable struct Data3{Float <: AbstractFloat}
length::Float"m"
end
d3 = Data3{Float64}(2.0u"mm")
@show d3.length