JuliaPhysics / JuliaPhysics/Unitful.jl
Convenient type annotation for struct fields
- Dominant language
- Julia
- Stars
- 675
- Forks
- 124
- Avg merge
- 3h 38m
- Merged PRs (30d)
- 1
Description
I've been trying out Unitful lately, and I found myself wanting to build a `struct` whose members were `Quantity` objects. But I'd like to specify their type and units.
Using the `Quantity` type directly is a bit awkward:
```julia
struct State
altitude::Quantity{Float64, typeof(dimension(u"m")), typeof(u"m")}
end
```
One way I worked out was:
```julia
struct State
altitude::typeof(1.0u"m")
end
```
But I'd rather say `Float64` than `1.0`. So then I thought about creating a function to generate the `Quantity` type for me:
```julia
quantity(T::DataType, u::Unitful.Units) = Quantity{T, typeof(dimension(u)), typeof(u)}
struct State
altitude::quantity(Float64, u"m")
end
```
Is there a convenient way to do this that I am missing? If not, should something like this `quantity` function be added?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.