Skip to content

Commit 7d43657

Browse files
committed
Define constants for complex subtypes
1 parent 4a958ee commit 7d43657

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/fmt.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,20 @@ end
7373
# methods to get the current default objects
7474
# note: if you want to set a default for an abstract type (i.e. AbstractFloat)
7575
# you'll need to extend this method like here:
76+
77+
const ComplexInteger = Complex{<:Integer}
78+
const ComplexFloat = Complex{<:AbstractFloat}
79+
const ComplexRational = Complex{<:Rational}
80+
7681
default_spec(::Type{<:Integer}) = DEFAULT_FORMATTERS[Integer]
7782
default_spec(::Type{<:AbstractFloat}) = DEFAULT_FORMATTERS[AbstractFloat]
7883
default_spec(::Type{<:AbstractString}) = DEFAULT_FORMATTERS[AbstractString]
7984
default_spec(::Type{<:AbstractChar}) = DEFAULT_FORMATTERS[AbstractChar]
8085
default_spec(::Type{<:AbstractIrrational}) = DEFAULT_FORMATTERS[AbstractIrrational]
8186
default_spec(::Type{<:Number}) = DEFAULT_FORMATTERS[Number]
82-
default_spec(::Complex{T} where T<:Integer) = DEFAULT_FORMATTERS[Complex{T} where T<:Integer]
83-
default_spec(::Complex{T} where T<:AbstractFloat) = DEFAULT_FORMATTERS[Complex{T} where T<:AbstractFloat]
84-
default_spec(::Complex{T} where T<:Rational) = DEFAULT_FORMATTERS[Complex{T} where T<:Rational]
87+
default_spec(::ComplexInteger) = DEFAULT_FORMATTERS[ComplexInteger]
88+
default_spec(::ComplexFloat) = DEFAULT_FORMATTERS[ComplexFloat]
89+
default_spec(::ComplexRational) = DEFAULT_FORMATTERS[ComplexRational]
8590

8691
default_spec(::Type{T}) where {T} =
8792
get(DEFAULT_FORMATTERS, T) do
@@ -224,11 +229,12 @@ for (t, c) in [(Integer,'d'),
224229
(AbstractFloat,'f'),
225230
(AbstractChar,'c'),
226231
(AbstractString,'s'),
227-
(Complex{T} where T<:Integer, 'd' ),
228-
(Complex{T} where T<:AbstractFloat, 'f' )]
232+
(ComplexInteger,'d'),
233+
(ComplexFloat,'f')]
229234
default_spec!(t, c)
230235
end
231236

232-
default_spec!(Number, 's', :right)
237+
default_spec!(Rational, 's', :right)
233238
default_spec!(AbstractIrrational, 's', :right)
234-
default_spec!(Complex{T} where T<:Rational, 's', :right)
239+
default_spec!(ComplexRational, 's', :right)
240+
default_spec!(Number, 's', :right)

test/fmt.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ i = 1234567
2727
@test fmt(1//2 + 6//2 * im, 15) == " 1//2 + 3//1*im"
2828

2929
fmt_default!(Rational, 'f', prec = 2)
30-
fmt_default!(Complex{T} where T<:Rational, 'f', prec = 2)
30+
fmt_default!(Format.ComplexRational, 'f', prec = 2)
3131

3232
@test fmt(3//4, 10, 2) == " 0.75"
3333
@test fmt(3//4, 10, 1) == " 0.8"

0 commit comments

Comments
 (0)