Skip to content

Commit

Permalink
make general case error and AbstractChar work
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger-luo committed Feb 25, 2022
1 parent a7ed7dd commit ecb8880
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Measurements.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,19 @@ function Measurement(val::V, err::E, tag::UInt64,
return Measurement(T(val), T(err), tag, Derivatives{T}(der))
end
Measurement{T}(x::Measurement{S}) where {T,S} = convert(Measurement{T}, x)
Measurement{T}(x::S) where {T,S} = convert(Measurement{T}, x)
Measurement{T}(x::S) where {T,S <: Real} = convert(Measurement{T}, x)

# disambiguities
Measurement{T}(x::S) where {T, S<:Rational} = convert(Measurement{T}, x)
Measurement{T}(x::S) where {T, S<:Complex} = convert(Measurement{T}, x)
Measurement{T}(x::S) where {T, S<:Base.TwicePrecision} = convert(Measurement{T}, x)
Measurement{T}(x::S) where {P, T, S<:Rational{P}} = convert(Measurement{T}, x)
Measurement{T}(x::S) where {T, S <: AbstractChar} = convert(Measurement{T}, x)


function Measurement{T}(::S) where {T, S<:AbstractChar}
function Measurement{T}(::S) where {T, S}
throw(ArgumentError("cannot convert `$S` to `Measurement{$T}`"))
end



# Functions to quickly create an empty Derivatives object.
@generated empty_der1(x::Measurement{T}) where {T<:AbstractFloat} = Derivatives{T}()
@generated empty_der2(x::T) where {T<:AbstractFloat} = Derivatives{x}()
Expand Down
2 changes: 2 additions & 0 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Base.convert(::Type{Measurement{T}}, a::Real) where {T<:AbstractFloat} =
measurement(T(a))::Measurement{T}
Base.convert(::Type{Measurement{T}}, a::Base.TwicePrecision) where {T<:AbstractFloat} =
measurement(T(a))::Measurement{T}
Base.convert(::Type{Measurement{T}}, a::AbstractChar) where {T<:AbstractFloat} =
measurement(T(a))::Measurement{T}

function Base.convert(::Type{Measurement{T}}, a::Complex) where {T}
if isreal(a)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ end
@test convert(Measurement{Float64}, 1+0im) 1.0±0.0
@test_throws InexactError convert(Measurement{Float64}, 1+1im)
@test convert(Measurement{Float64}, Base.TwicePrecision(1.0, 0.0)) 1.0±0.0
@test convert(Measurement{Float64}, 'a') Float64('a') ± 0.0
end

@testset "Comparisons and Tests" begin
Expand Down

0 comments on commit ecb8880

Please sign in to comment.