# Structs with Unitful Types

**URL:** <https://discourse.julialang.org/t/structs-with-unitful-types/115884>\
**Category:** General Usage\
**Created:** [June 19, 2024, 8:33pm UTC](https://discourse.julialang.org/t/structs-with-unitful-types/115884 "2024-06-19T20:33:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [June 19, 2024, 8:33pm UTC](https://discourse.julialang.org/t/structs-with-unitful-types/115884/1 "2024-06-19T20:33:56Z")

</div>

I am creating a number of structs with unitful types. Here is a small example:

```julia
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

```julia
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

```julia
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?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [June 19, 2024, 9:16pm UTC](https://discourse.julialang.org/t/structs-with-unitful-types/115884/2 "2024-06-19T21:16:28Z")

</div>

We solved that issue in Meshes.jl where all our structs now have units, but we still allow non-unitful constructors (which default to meter units). Maybe you can take a look into our source code to learn some of the tricks? @eliascarv can share more details if you have further questions.

Perhaps all you need are “geometries” in your application? Is a “slab” a piece of stone with given shape? I wonder if you could simply use Meshes.jl to represent these. You can create aliases in your application like `const Slab = Hexahedron` to still use “jargon” names.

---

<div class="post-metadata">

**Author:** ![co1emi11er](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/co1emi11er/32/206849_2.png) [@co1emi11er](https://discourse.julialang.org/u/co1emi11er)\
**Post date:** [June 19, 2024, 9:34pm UTC](https://discourse.julialang.org/t/structs-with-unitful-types/115884/3 "2024-06-19T21:34:05Z")

</div>

Thanks I will look into the Meshes.jl code. A slab is like a deck of a bridge. Which could easily be defined with an object that is probably present in Meshes.jl. The girders I am just drawing from a set of x and y points.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/1/e11d9314b8bfc3f3b6896ebc524b3d23d1bff930.png)

I will probably try to use Meshes.jl eventualy if I can ever get to it. There is a python package called [sectionproperties](https://sectionproperties.readthedocs.io/en/latest/examples.html) that makes use of shapley and a mesh package to define section properties of shapes. I am curious if something similar could be made in Julia.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [June 19, 2024, 9:39pm UTC](https://discourse.julialang.org/t/structs-with-unitful-types/115884/4 "2024-06-19T21:39:49Z")

</div>

Doesn’t this work?

```julia
slab(;a=da, b=db, c=dc) = Slab(uconvert(A, a), uconvert(B, b), uconvert(C, c))

```
