Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,22 @@ julia> unit(1.0) == NoUnits
true
```
"""

"""
This is shown if a user has not defined units for their quantity type.
"""
const MISSING_UNIT_DEFINITION_MESSAGE =
"""
You tried calling unit on an object that has none defined.
Not everything has (or should have) units.
If your type should have units, define a method of unit on them.
"""

@inline unit(x::Any) = throw( DomainError( x, MISSING_UNIT_DEFINITION_MESSAGE ) )
@inline unit(x::Type{Any}) = throw( DomainError( x, MISSING_UNIT_DEFINITION_MESSAGE ) )
@inline unit(x::Number) = NoUnits
@inline unit(x::Type{T}) where {T <: Number} = NoUnits
@inline unit(x::Type{Union{Missing, T}}) where T = unit(T)
@inline unit(x::Type{Union{Missing, T}}) where T = unit(T) # Type restriction required here for T, or might result in infinite recursion.
@inline unit(x::Type{Missing}) = missing
@inline unit(x::Missing) = missing

Expand Down
6 changes: 3 additions & 3 deletions test/dates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
@test_throws MethodError dimension(T(1))
@test_throws MethodError Unitful.numtype(T)
@test_throws MethodError Unitful.numtype(T(1))
@test_throws MethodError unit(T)
@test_throws MethodError unit(T(1))
@test_throws DomainError unit(T)
@test_throws DomainError unit(T(1))
end

for p = (CompoundPeriod, CompoundPeriod(), CompoundPeriod(Day(1)), CompoundPeriod(Day(1), Hour(-1)))
@test dimension(p) === 𝐓
@test_throws MethodError Unitful.numtype(p)
@test_throws MethodError unit(p)
@test_throws DomainError unit(p)
end
end

Expand Down
Loading