Skip to content

Commit

Permalink
Add constructorof for Quantity, and ConstructionBase dep (PainterQubi…
Browse files Browse the repository at this point in the history
…ts#280)

* add ConstructionBase dep and constructorof for Quantity

* update compat and CI versions

* allow failures on nightly
  • Loading branch information
rafaqz authored and ajkeller34 committed Nov 21, 2019
1 parent 7ff5c0d commit 84c6451
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 7 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ os:
- linux
# - osx # locally test on my MacBook Pro; OS X tests take too long on Travis CI
julia:
- 0.7
- 1.0
- 1.1
- 1.2
- nightly
notifications:
email: false
# matrix:
# allow_failures:
# - julia: nightly
matrix:
allow_failures:
- julia: nightly
# uncomment the following lines to override the default test script
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
Expand Down
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ uuid = "1986cc42-f94f-5a68-af5c-568840ba703d"
version = "0.17.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
julia = "≥ 0.7.0"
julia = "1"
ConstructionBase = "1"

[extras]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
1 change: 0 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
environment:
matrix:
- julia_version: 0.7
- julia_version: 1
- julia_version: 1.1
- julia_version: 1.2
Expand Down
2 changes: 2 additions & 0 deletions src/Unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import LinearAlgebra: Diagonal, Bidiagonal, Tridiagonal, SymTridiagonal
import LinearAlgebra: istril, istriu, norm
import Random

import ConstructionBase: constructorof

export logunit, unit, absoluteunit, dimension, uconvert, ustrip, upreferred
export @dimension, @derived_dimension, @refunit, @unit, @affineunit, @u_str
export Quantity, DimensionlessQuantity, NoUnits, NoDims
Expand Down
6 changes: 6 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ struct Quantity{T,D,U} <: AbstractQuantity{T,D,U}
Quantity{T,D,U}(v::Quantity) where {T,D,U} = convert(Quantity{T,D,U}, v)
end

# Field-only constructor
Quantity{<:Any,D,U}(val) where {D,U} = Quantity{typeof(val),D,U}(val)

constructorof(::Type{Unitful.Quantity{_,D,U}}) where {_,D,U} =
Unitful.Quantity{T,D,U} where T

"""
DimensionlessUnits{U}
Useful for dispatching on [`Unitful.Units`](@ref) types that have no dimensions.
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Unitful
using Test, LinearAlgebra, Random
using Test, LinearAlgebra, Random, ConstructionBase
import Unitful: DimensionError, AffineError
import Unitful: LogScaled, LogInfo, Level, Gain, MixedUnits, Decibel
import Unitful: FreeUnits, ContextUnits, FixedUnits, AffineUnits, AffineQuantity
Expand Down Expand Up @@ -75,6 +75,7 @@ const colon = Base.:(:)
@test ContextUnits(m, FixedUnits(mm)) === ContextUnits(m, mm)
@test ContextUnits(m, ContextUnits(mm, mm)) === ContextUnits(m, mm)
@test_throws DimensionError ContextUnits(m,kg)
@test ConstructionBase.constructorof(typeof(1.0m))(2) === 2m
end

@testset "Types" begin
Expand Down

0 comments on commit 84c6451

Please sign in to comment.