I am creating a number of structs with unitful types. Here is a small example:
const float_ft = typeof(1.0ft) # ft, inch, and kcf previously defined constants
const float_inch = typeof(1.0inch)
const float_kcf = typeof(1.0kcf)
@with_kw struct Slab
width::float_ft
thickness::float_inch = 8.5inch
fws_thickness::float_inch = 0.0inch
fws_density::float_kcf = 0.15kcf
cross_slope::Float64 = 0.0
end
s = Slab(40ft) #will convert to Slab(40.0ft) (rest of fields left out for clarity)
This works great when I pass in unitful types. However, I foresee pulling information from excel or maybe from a gui where I will have to convert from a number or string to a number with the proper unit. Like so:
From Excel
width = XLSX.readdata("myfile.xlsx", "mysheet!A1") * ft
thickness = XLSX.readdata("myfile.xlsx", "mysheet!A1") * inch
# ... rest of code parsing
Slab(width, thickness, fws_thickness, fws_density, cross_slope)
From GUI
width = parse(Float64, tb_width) * ft # tb_width is some textbox with string number
# ... rest of code
I would like for the constructor to convert to the proper unit no matter the number of arguments. So Slab(40.0)
would correctly create the Slab(40.0ft)
(rest of fields left out for clarity). I assume this would need to be an inner constructor, but not sure how I would do that in a robust way.
I have many structs and about 10 different units so far, so it seems like the amount of boiler plate code is going to get bigger and bigger as well as the potential error.
I am curious if anyone else has dealt with this and any possible solutions?