Skip to content

Commit 88da738

Browse files
committed
Make generic constructors for Dimension
1 parent de11481 commit 88da738

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

src/types.jl

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ const DEFAULT_VALUE_TYPE = Float64
44
abstract type AbstractQuantity{T,R} end
55
abstract type AbstractDimensions{R} end
66

7+
constructor_of(::Type{D}) where {D<:AbstractDimensions} = D
8+
constructor_of(::Type{D}) where {R,D<:AbstractDimensions{R}} = D.name.wrapper
9+
constructor_of(::Type{Q}) where {Q<:AbstractQuantity} = Q
10+
constructor_of(::Type{Q}) where {T,Q<:AbstractQuantity{T}} = Q.body.name.wrapper
11+
constructor_of(::Type{Q}) where {T,R,Q<:AbstractQuantity{T,R}} = Q.name.wrapper
12+
713
"""
814
Dimensions
915
@@ -29,31 +35,15 @@ struct Dimensions{R<:Real} <: AbstractDimensions{R}
2935
temperature::R
3036
luminosity::R
3137
amount::R
32-
33-
function Dimensions(length::_R,
34-
mass::_R,
35-
time::_R,
36-
current::_R,
37-
temperature::_R,
38-
luminosity::_R,
39-
amount::_R) where {_R<:Real}
40-
new{_R}(length, mass, time, current, temperature, luminosity, amount)
41-
end
42-
Dimensions(; kws...) = Dimensions(DEFAULT_DIM_TYPE; kws...)
43-
Dimensions(::Type{_R}; kws...) where {_R} = Dimensions(
44-
tryrationalize(_R, get(kws, :length, zero(_R))),
45-
tryrationalize(_R, get(kws, :mass, zero(_R))),
46-
tryrationalize(_R, get(kws, :time, zero(_R))),
47-
tryrationalize(_R, get(kws, :current, zero(_R))),
48-
tryrationalize(_R, get(kws, :temperature, zero(_R))),
49-
tryrationalize(_R, get(kws, :luminosity, zero(_R))),
50-
tryrationalize(_R, get(kws, :amount, zero(_R))),
51-
)
52-
Dimensions{_R}(; kws...) where {_R} = Dimensions(_R; kws...)
53-
Dimensions{_R}(args...) where {_R} = Dimensions(Base.Fix1(convert, _R).(args)...)
54-
Dimensions{_R}(d::Dimensions) where {_R} = Dimensions{_R}(d.length, d.mass, d.time, d.current, d.temperature, d.luminosity, d.amount)
5538
end
5639

40+
(::Type{D})(::Type{R}; kws...) where {R,D<:AbstractDimensions} = D{R}((tryrationalize(R, get(kws, k, zero(R))) for k in fieldnames(D))...)
41+
(::Type{D})(; kws...) where {D<:AbstractDimensions} = D(DEFAULT_DIM_TYPE; kws...)
42+
43+
(::Type{D})(args...) where {R,D<:AbstractDimensions{R}} = constructor_of(D)(Base.Fix1(convert, R).(args)...)
44+
(::Type{D})(; kws...) where {R,D<:AbstractDimensions{R}} = constructor_of(D)(R; kws...)
45+
(::Type{D})(d::AbstractDimensions) where {R,D<:AbstractDimensions{R}} = D((getfield(d, k) for k in fieldnames(D))...)
46+
5747

5848
"""
5949
Quantity{T}

0 commit comments

Comments
 (0)