Skip to content

Add Base.zero and one #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Base.iterate(d::Dimensions) = (d, nothing)
Base.iterate(::Dimensions, ::Nothing) = nothing
Base.iterate(q::Quantity) = (q, nothing)
Base.iterate(::Quantity, ::Nothing) = nothing
Base.zero(::Type{Quantity{T}}) where {T} = Quantity(zero(T))
Base.one(::Type{Quantity{T}}) where {T} = Quantity(one(T))
Base.one(::Type{Dimensions}) = Dimensions()

Base.show(io::IO, d::Dimensions) =
let tmp_io = IOBuffer()
Expand Down
7 changes: 7 additions & 0 deletions test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ using Test

@test abs(x) == Quantity(1.2, length=2 // 5)
@test abs(x) == abs(Quantity(1.2, length=2 // 5))

@test one(Quantity{Float64}) == Quantity(1.0)
@test one(Quantity{String}) == Quantity("")
@test zero(Quantity{Float64}) == Quantity(0.0)
@test zero(Quantity{Int}) == Quantity(0, length=0, mass=0)
@test Quantity(1.0, one(Dimensions)) == Quantity(1.0)
@test one(Dimensions) == Dimensions()
end

@testset "Fallbacks" begin
Expand Down