Skip to content

Commit de11481

Browse files
committed
Remove redundancy in abstract type interface
1 parent 26355cc commit de11481

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

src/types.jl

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ struct Dimensions{R<:Real} <: AbstractDimensions{R}
5454
Dimensions{_R}(d::Dimensions) where {_R} = Dimensions{_R}(d.length, d.mass, d.time, d.current, d.temperature, d.luminosity, d.amount)
5555
end
5656

57-
new_dimensions(::Type{<:Dimensions}, l...) = Dimensions(l...)
58-
59-
const DIMENSION_NAMES = Base.fieldnames(Dimensions)
60-
const DIMENSION_SYNONYMS = (:m, :kg, :s, :A, :K, :cd, :mol)
61-
const SYNONYM_MAPPING = NamedTuple(DIMENSION_NAMES .=> DIMENSION_SYNONYMS)
6257

6358
"""
6459
Quantity{T}
@@ -89,8 +84,10 @@ struct Quantity{T,R} <: AbstractQuantity{T,R}
8984
Quantity{T,R}(q::Quantity) where {T,R} = Quantity(convert(T, q.value), Dimensions{R}(dimension(q)))
9085
end
9186

92-
new_quantity(::Type{<:Quantity}, value, dim) = Quantity(value, dim)
93-
new_quantity(::Type{<:Dimensions}, value, dim) = Quantity(value, dim)
87+
# All that is needed to make an `AbstractQuantity` work:
88+
dimension_name(::Dimensions, k::Symbol) = (length="m", mass="kg", time="s", current="A", temperature="K", luminosity="cd", amount="mol")[k]
89+
new_dimensions(::Type{<:Dimensions}, dims...) = Dimensions(dims...)
90+
new_quantity(::Type{<:Union{<:Quantity,<:Dimensions}}, l, r) = Quantity(l, r)
9491

9592
struct DimensionError{Q1,Q2} <: Exception
9693
q1::Q1

src/utils.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ Base.oneunit(::Dimensions) = error("There is no such thing as a dimensionful 1 f
6969
Base.oneunit(::Type{<:Quantity}) = error("Cannot create a dimensionful 1 for a `Quantity` type without knowing the dimensions. Please use `oneunit(::Quantity)` instead.")
7070
Base.oneunit(::Type{<:Dimensions}) = error("There is no such thing as a dimensionful 1 for a `Dimensions` type, as + is only defined for `Quantity`.")
7171

72-
dimension_name(::Dimensions, k::Symbol) = SYNONYM_MAPPING[k]
7372
Base.show(io::IO, d::AbstractDimensions) =
7473
let tmp_io = IOBuffer()
7574
for k in keys(d)

test/unittests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using DynamicQuantities
2-
using DynamicQuantities: DEFAULT_DIM_TYPE, DEFAULT_VALUE_TYPE, DIMENSION_NAMES
2+
using DynamicQuantities: DEFAULT_DIM_TYPE, DEFAULT_VALUE_TYPE
33
using Ratios: SimpleRatio
44
using SaferIntegers: SafeInt16
55
using Test
@@ -182,7 +182,7 @@ end
182182
@test Quantity(0.5, length=2) / Dimensions(length=1) == Quantity(0.5, length=1)
183183
@test Dimensions(length=1) / Quantity(0.5, length=2, mass=-5) == Quantity(2, length=-1, mass=5)
184184

185-
@test Dimensions{Int8}([0 for i=1:length(DIMENSION_NAMES)]...) == Dimensions{Int8}()
185+
@test Dimensions{Int8}(zeros(Int, 7)...) == Dimensions{Int8}()
186186

187187
@test zero(Quantity(0.0+0.0im)) + Quantity(1) == Quantity(1.0+0.0im, length=Int8(0))
188188
@test oneunit(Quantity(0.0+0.0im)) - Quantity(1) == Quantity(0.0+0.0im, length=Int8(0))

0 commit comments

Comments
 (0)