String macro to simplify type declarations with units

Apologies, yes I had a silly moment there.

He means you should interpolate esc(:Float) into the expression. e.g.

julia> using Unitful

julia> macro Float_str(str)
           :(Quantity{$(esc(:Float)), dimension(@u_str($str)), typeof(@u_str($str))})
       end
@Float_str (macro with 1 method)

julia> mutable struct Data3{Float}
           length::Float"m"
       end

julia> d3 = Data3{Float64}(2.0u"mm")
Data3{Float64}(0.002 m)

But yeah, prt of why I got confused is that this macro is very specialized, it’s written assuming that the user has defined a variable Float outside of the macro, which is usually a bad idea.

1 Like