Skip to content

Commit cb40722

Browse files
authored
Uniformize CartesianSpace and ComplexSpace constructors (#334)
1 parent 8d63119 commit cb40722

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

src/spaces/cartesianspace.jl

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,31 @@ vector space that is implicitly assumed in most of matrix algebra.
1111
"""
1212
struct CartesianSpace <: ElementarySpace
1313
d::Int
14+
15+
# required to avoid CartesianSpace(::Any) default constructor:
16+
CartesianSpace(d::Int) = new(d)
1417
end
18+
1519
CartesianSpace(d::Integer = 0; dual = false) = CartesianSpace(Int(d))
16-
function CartesianSpace(dim::Pair; dual = false)
17-
if dim.first === Trivial()
18-
return CartesianSpace(dim.second; dual = dual)
19-
else
20-
msg = "$(dim) is not a valid dimension for CartesianSpace"
21-
throw(SectorMismatch(msg))
22-
end
23-
end
24-
function CartesianSpace(dims::AbstractDict; kwargs...)
25-
if length(dims) == 0
26-
return CartesianSpace(0; kwargs...)
27-
elseif length(dims) == 1
28-
return CartesianSpace(first(dims); kwargs...)
29-
else
30-
msg = "$(dims) is not a valid dimension dictionary for CartesianSpace"
31-
throw(SectorMismatch(msg))
32-
end
20+
CartesianSpace(dim::Pair; kwargs...) = CartesianSpace((dim,); kwargs...)
21+
function CartesianSpace(dims; dual::Bool = false)
22+
# using manual iteration here to avoid depending on `length` while still checking it is
23+
# 0 ≤ length ≤ 1
24+
next = Base.iterate(dims)
25+
isnothing(next) && return CartesianSpace(0)
26+
27+
(c, d), state = next
28+
convert(Trivial, c) === Trivial() ||
29+
throw(SectorMismatch(lazy"$c is not a valid charge for CartesianSpace"))
30+
31+
V = CartesianSpace(d)
32+
33+
next = Base.iterate(dims, state)
34+
isnothing(next) ||
35+
throw(SectorMismatch(lazy"$dims is not a valid dimension iterable for CartesianSpace"))
36+
37+
return V
3338
end
34-
CartesianSpace(g::Base.Generator; kwargs...) = CartesianSpace(g...; kwargs...)
3539

3640
# convenience constructor
3741
Base.getindex(::RealNumbers) = CartesianSpace

src/spaces/complexspace.jl

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,27 @@ struct ComplexSpace <: ElementarySpace
1111
d::Int
1212
dual::Bool
1313
end
14-
ComplexSpace(d::Integer = 0; dual = false) = ComplexSpace(Int(d), dual)
15-
function ComplexSpace(dim::Pair; dual = false)
16-
if dim.first === Trivial()
17-
return ComplexSpace(dim.second; dual = dual)
18-
else
19-
msg = "$(dim) is not a valid dimension for ComplexSpace"
20-
throw(SectorMismatch(msg))
21-
end
22-
end
23-
function ComplexSpace(dims::AbstractDict; kwargs...)
24-
if length(dims) == 0
25-
return ComplexSpace(0; kwargs...)
26-
elseif length(dims) == 1
27-
return ComplexSpace(first(dims); kwargs...)
28-
else
29-
msg = "$(dims) is not a valid dimension dictionary for ComplexSpace"
30-
throw(SectorMismatch(msg))
31-
end
14+
15+
ComplexSpace(d::Integer = 0; dual::Bool = false) = ComplexSpace(Int(d), dual)
16+
ComplexSpace(dim::Pair; kwargs...) = ComplexSpace((dim,); kwargs...)
17+
function ComplexSpace(dims; dual::Bool = false)
18+
# using manual iteration here to avoid depending on `length` while still checking it is
19+
# 0 ≤ length ≤ 1
20+
next = Base.iterate(dims)
21+
isnothing(next) && return ComplexSpace(0, dual)
22+
23+
(c, d), state = next
24+
convert(Trivial, c) === Trivial() ||
25+
throw(SectorMismatch(lazy"$c is not a valid charge for ComplexSpace"))
26+
27+
V = ComplexSpace(d, dual)
28+
29+
next = Base.iterate(dims, state)
30+
isnothing(next) ||
31+
throw(SectorMismatch(lazy"$dims is not a valid dimension iterable for ComplexSpace"))
32+
33+
return V
3234
end
33-
ComplexSpace(g::Base.Generator; kwargs...) = ComplexSpace(g...; kwargs...)
3435

3536
# convenience constructor
3637
Base.getindex(::ComplexNumbers) = ComplexSpace

0 commit comments

Comments
 (0)